added 'add new label' support

This commit is contained in:
Luis Ángel San Martín 2014-11-11 15:46:30 +01:00
parent a74309aed9
commit d1fbb1d5ba
12 changed files with 189 additions and 10 deletions

View File

@ -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);
}

View File

@ -3,16 +3,22 @@
#include <QtWidgets>
#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;

View File

@ -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<QVariant> &data, ReadingListItem *p)

View File

@ -4,6 +4,9 @@
#include <QIcon>
#include <QVariant>
#include "yacreader_global.h"
//TODO add propper constructors, using QList<QVariant> is not safe
class ListItem
{
public:
@ -31,6 +34,8 @@ class LabelItem : public ListItem
public:
LabelItem(const QList<QVariant> &data);
QIcon getIcon() const;
YACReader::LabelColors colorid();
QString name();
};
//------------------------------------------------------

View File

@ -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<QVariant>() << 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<LabelItem *> 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<QVariant>() << record.value("name") << record.value("color") << record.value("id"));
list << new LabelItem(QList<QVariant>() << 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<QVariant>() /*<< 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;
}

View File

@ -7,6 +7,8 @@
#include <QSqlQuery>
#include <QSqlDatabase>
#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<SpecialListItem *> setupSpecialLists(QSqlDatabase &db);
QList<LabelItem *> setupLabels(QSqlDatabase &db);
void setupReadingLists(QSqlDatabase &db);
int addLabelIntoList(LabelItem *item);
//Special lists
QList<SpecialListItem *> specialLists;

View File

@ -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<LibraryItem *> DBHelper::getFoldersFromParent(qulonglong parentId, QSqlDatabase & db, bool sort)

View File

@ -4,6 +4,7 @@
class QString;
#include <QMap>
#include <QList>
#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);

View File

@ -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)

View File

@ -369,7 +369,8 @@ public slots:
void errorDeletingFolder();
void addNewReadingList();
void deleteSelectedReadingList();
void addNewLabel();
void showAddNewLabelDialog();
};
#endif

View File

@ -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";
}
}

View File

@ -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