mirror of
https://github.com/YACReader/yacreader
synced 2025-07-23 23:44:52 -04:00
added 'add new label' support
This commit is contained in:
@ -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)
|
||||
|
@ -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();
|
||||
};
|
||||
|
||||
//------------------------------------------------------
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user