From d1fbb1d5ba7af3fbdd4be1bd580363ccecc69470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Tue, 11 Nov 2014 15:46:30 +0100 Subject: [PATCH] added 'add new label' support --- YACReaderLibrary/add_label_dialog.cpp | 32 +++++++++++++- YACReaderLibrary/add_label_dialog.h | 10 ++++- YACReaderLibrary/db/reading_list_item.cpp | 16 +++++++ YACReaderLibrary/db/reading_list_item.h | 5 +++ YACReaderLibrary/db/reading_list_model.cpp | 51 +++++++++++++++++++++- YACReaderLibrary/db/reading_list_model.h | 5 +++ YACReaderLibrary/db_helper.cpp | 14 +++++- YACReaderLibrary/db_helper.h | 2 + YACReaderLibrary/library_window.cpp | 14 ++++-- YACReaderLibrary/library_window.h | 3 +- common/yacreader_global.cpp | 31 +++++++++++++ common/yacreader_global.h | 16 +++++++ 12 files changed, 189 insertions(+), 10 deletions(-) diff --git a/YACReaderLibrary/add_label_dialog.cpp b/YACReaderLibrary/add_label_dialog.cpp index 99a6f5d8..098d92b4 100644 --- a/YACReaderLibrary/add_label_dialog.cpp +++ b/YACReaderLibrary/add_label_dialog.cpp @@ -45,12 +45,40 @@ AddLabelDialog::AddLabelDialog(QWidget *parent) : layout->addLayout(buttons); setLayout(layout); + + //connections + connect(edit,SIGNAL(textChanged(QString)),this,SLOT(validateName(QString))); + connect(cancelButton,SIGNAL(clicked()),this,SLOT(close())); + connect(acceptButton,SIGNAL(clicked()),this,SLOT(accept())); + } -void AddLabelDialog::open() +YACReader::LabelColors AddLabelDialog::selectedColor() { - QDialog::open(); + return YACReader::LabelColors(list->currentRow()); +} +QString AddLabelDialog::name() +{ + return edit->text(); +} + +int AddLabelDialog::exec() +{ edit->clear(); list->clearSelection(); + + acceptButton->setDisabled(true); + + list->setCurrentRow(0); + + return QDialog::exec(); +} + +void AddLabelDialog::validateName(const QString &name) +{ + if(name.isEmpty()) + acceptButton->setDisabled(true); + else + acceptButton->setEnabled(true); } diff --git a/YACReaderLibrary/add_label_dialog.h b/YACReaderLibrary/add_label_dialog.h index 67078227..1f5de48b 100644 --- a/YACReaderLibrary/add_label_dialog.h +++ b/YACReaderLibrary/add_label_dialog.h @@ -3,16 +3,22 @@ #include +#include "yacreader_global.h" + class AddLabelDialog : public QDialog { Q_OBJECT public: explicit AddLabelDialog(QWidget *parent = 0); - + YACReader::LabelColors selectedColor(); + QString name(); signals: public slots: - void open(); + int exec(); + +protected slots: + void validateName(const QString & name); protected: QLineEdit * edit; diff --git a/YACReaderLibrary/db/reading_list_item.cpp b/YACReaderLibrary/db/reading_list_item.cpp index c86143d7..4cc123d4 100644 --- a/YACReaderLibrary/db/reading_list_item.cpp +++ b/YACReaderLibrary/db/reading_list_item.cpp @@ -50,6 +50,22 @@ QIcon LabelItem::getIcon() const } } +YACReader::LabelColors LabelItem::colorid() +{ + if(itemData.count()>3) + { + return YACReader::LabelColors(itemData.at(3).toInt()); + } +} + +QString LabelItem::name() +{ + if(itemData.count()>0) + { + return itemData.at(0).toString(); + } +} + //------------------------------------------------------ ReadingListItem::ReadingListItem(const QList &data, ReadingListItem *p) diff --git a/YACReaderLibrary/db/reading_list_item.h b/YACReaderLibrary/db/reading_list_item.h index fdb50e34..4cdbeb32 100644 --- a/YACReaderLibrary/db/reading_list_item.h +++ b/YACReaderLibrary/db/reading_list_item.h @@ -4,6 +4,9 @@ #include #include +#include "yacreader_global.h" +//TODO add propper constructors, using QList is not safe + class ListItem { public: @@ -31,6 +34,8 @@ class LabelItem : public ListItem public: LabelItem(const QList &data); QIcon getIcon() const; + YACReader::LabelColors colorid(); + QString name(); }; //------------------------------------------------------ diff --git a/YACReaderLibrary/db/reading_list_model.cpp b/YACReaderLibrary/db/reading_list_model.cpp index 94069fd1..c6bee22b 100644 --- a/YACReaderLibrary/db/reading_list_model.cpp +++ b/YACReaderLibrary/db/reading_list_model.cpp @@ -3,6 +3,8 @@ #include "reading_list_item.h" #include "data_base_management.h" +#include "qnaturalsorting.h" +#include "db_helper.h" #include "QsLog.h" @@ -181,6 +183,19 @@ void ReadingListModel::setupModelData(QString path) endResetModel(); } +void ReadingListModel::addNewLabel(const QString &name, YACReader::LabelColors color) +{ + QSqlDatabase db = DataBaseManagement::loadDatabase(_databasePath); + qulonglong id = DBHelper::insertLabel(name, color, db); + + int newPos = addLabelIntoList(new LabelItem(QList() << name << YACReader::colorToName(color) << id << color)); + + beginInsertRows(QModelIndex(),specialLists.count()+1+newPos+1, specialLists.count()+1+newPos+1); + endInsertRows(); + + QSqlDatabase::removeDatabase(_databasePath); +} + void ReadingListModel::deleteItem(const QModelIndex &mi) { @@ -232,7 +247,7 @@ QList ReadingListModel::setupLabels(QSqlDatabase & db) QSqlQuery selectQuery("SELECT * FROM label ORDER BY ordering,name",db); //TODO add some kind of while(selectQuery.next()) { QSqlRecord record = selectQuery.record(); - list << new LabelItem(QList() << record.value("name") << record.value("color") << record.value("id")); + list << new LabelItem(QList() << record.value("name") << record.value("color") << record.value("id") << record.value("ordering")); } //TEST @@ -266,4 +281,38 @@ void ReadingListModel::setupReadingLists(QSqlDatabase & db) node1->appendChild(new ReadingListItem(QList() /*<< 0*/ << "sublist" << "atr",node1)); } +int ReadingListModel::addLabelIntoList(LabelItem *item) +{ + if(labels.isEmpty()) + labels << item; + else + { + int i = 0; + + while (i < labels.count() && (labels.at(i)->colorid() < item->colorid()) ) + i++; + + if(i < labels.count()) + { + if(labels.at(i)->colorid() == item->colorid()) //sort by name + { + while( i < labels.count() && naturalSortLessThanCI(labels.at(i)->name(),item->name())) + i++; + } + } + + + if(i >= labels.count()) + labels << item; + else + { + labels.insert(i,item); + } + + return i; + } + + return 0; +} + diff --git a/YACReaderLibrary/db/reading_list_model.h b/YACReaderLibrary/db/reading_list_model.h index 3dbfcd8a..02ad61d8 100644 --- a/YACReaderLibrary/db/reading_list_model.h +++ b/YACReaderLibrary/db/reading_list_model.h @@ -7,6 +7,8 @@ #include #include +#include "yacreader_global.h" + class LabelItem; class SpecialListItem; class ReadingListItem; @@ -32,6 +34,8 @@ public: //Convenience methods void setupModelData(QString path); + void addNewLabel(const QString & name, YACReader::LabelColors color); + signals: public slots: @@ -43,6 +47,7 @@ private: QList setupSpecialLists(QSqlDatabase &db); QList setupLabels(QSqlDatabase &db); void setupReadingLists(QSqlDatabase &db); + int addLabelIntoList(LabelItem *item); //Special lists QList specialLists; diff --git a/YACReaderLibrary/db_helper.cpp b/YACReaderLibrary/db_helper.cpp index 64124760..d43e1bd4 100644 --- a/YACReaderLibrary/db_helper.cpp +++ b/YACReaderLibrary/db_helper.cpp @@ -351,7 +351,19 @@ qulonglong DBHelper::insert(ComicDB * comic, QSqlDatabase & db) query.bindValue(":name", comic->name); query.bindValue(":path", comic->path); query.exec(); - return query.lastInsertId().toULongLong(); + return query.lastInsertId().toULongLong(); +} + +qulonglong DBHelper::insertLabel(const QString &name, YACReader::LabelColors color, QSqlDatabase &db) +{ + QSqlQuery query(db); + query.prepare("INSERT INTO label (name, color, ordering) " + "VALUES (:name, :color, :ordering)"); + query.bindValue(":name", name); + query.bindValue(":color", YACReader::colorToName(color)); + query.bindValue(":ordering", color); + query.exec(); + return query.lastInsertId().toULongLong(); } //queries QList DBHelper::getFoldersFromParent(qulonglong parentId, QSqlDatabase & db, bool sort) diff --git a/YACReaderLibrary/db_helper.h b/YACReaderLibrary/db_helper.h index 4b310469..bb818b22 100644 --- a/YACReaderLibrary/db_helper.h +++ b/YACReaderLibrary/db_helper.h @@ -4,6 +4,7 @@ class QString; #include #include +#include "yacreader_global.h" class ComicDB; class Folder; @@ -36,6 +37,7 @@ public: //inserts static qulonglong insert(Folder * folder, QSqlDatabase & db); static qulonglong insert(ComicDB * comic, QSqlDatabase & db); + static qulonglong insertLabel(const QString & name, YACReader::LabelColors color , QSqlDatabase & db); //updates static void update(qulonglong libraryId, ComicInfo & comicInfo); static void update(ComicDB * comics, QSqlDatabase & db); diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index dbcc9fe3..efd1cdca 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -1153,7 +1153,7 @@ void LibraryWindow::createConnections() //lists connect(addReadingListAction,SIGNAL(triggered()),this,SLOT(addNewReadingList())); connect(deleteReadingListAction,SIGNAL(triggered()),this,SLOT(deleteSelectedReadingList())); - connect(addLabelAction,SIGNAL(triggered()),this,SLOT(addNewLabel())); + connect(addLabelAction,SIGNAL(triggered()),this,SLOT(showAddNewLabelDialog())); } void LibraryWindow::loadLibrary(const QString & name) @@ -1626,10 +1626,18 @@ void LibraryWindow::deleteSelectedReadingList() } -void LibraryWindow::addNewLabel() +void LibraryWindow::showAddNewLabelDialog() { AddLabelDialog * dialog = new AddLabelDialog(); - dialog->open(); + int ret = dialog->exec(); + + if (ret == QDialog::Accepted) + { + YACReader::LabelColors color = dialog->selectedColor(); + QString name = dialog->name(); + + listsModel->addNewLabel(name,color); + } } void LibraryWindow::selectSubfolder(const QModelIndex &mi, int child) diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index 9808f088..a8c588e8 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -369,7 +369,8 @@ public slots: void errorDeletingFolder(); void addNewReadingList(); void deleteSelectedReadingList(); - void addNewLabel(); + void showAddNewLabelDialog(); + }; #endif diff --git a/common/yacreader_global.cpp b/common/yacreader_global.cpp index c8ad06c3..d4ae0dd3 100644 --- a/common/yacreader_global.cpp +++ b/common/yacreader_global.cpp @@ -27,3 +27,34 @@ QAction * YACReader::createSeparator() a->setSeparator(true); return a; } + + +QString YACReader::colorToName(LabelColors colors) +{ + switch(colors){ + case 0: + return "red"; + case 1: + return "orange"; + case 2: + return "yellow"; + case 3: + return "green"; + case 4: + return "cyan"; + case 5: + return "blue"; + case 6: + return "violet"; + case 7: + return "purple"; + case 8: + return "pink"; + case 9: + return "white"; + case 10: + return "light"; + case 11: + return "dark"; + } +} diff --git a/common/yacreader_global.h b/common/yacreader_global.h index 86b24de0..94799358 100644 --- a/common/yacreader_global.h +++ b/common/yacreader_global.h @@ -112,9 +112,25 @@ namespace YACReader ByAuthor }; + enum LabelColors{ + YRed = 0, + YOrange, + YYellow, + YGreen, + YCyan, + YBlue, + YViolet, + YPurple, + YPink, + YWhite, + YLight, + YDark + }; + QString getSettingsPath(); void addSperator(QWidget * w); QAction * createSeparator(); +QString colorToName(LabelColors colors); } #endif