Permalink
Browse files

Rewrote Add/Remove tag methods

  • Loading branch information...
1 parent d2b2e1c commit 696fca4abaf71c02596c5d4f0768e8665fc2b6df Pierre Marchand committed Jan 11, 2011
Showing with 47 additions and 24 deletions.
  1. +12 −22 src/tagswidget.cpp
  2. +30 −2 src/tagswidget_listmodel.cpp
  3. +5 −0 src/tagswidget_listmodel.h
View
@@ -15,6 +15,7 @@
#include <QMenu>
#include <QDebug>
#include <QFont>
+#include <QModelIndex>
#include "typotek.h"
#include "tagswidget.h"
@@ -48,42 +49,31 @@ void TagsWidget::prepare(QList<FontItem *> fonts)
void TagsWidget::slotNewTag()
{
-
-// QString nTag(tr("New Tag"));
-// newTagBeingEdited = new QListWidgetItem(0);
-// newTagBeingEdited->setText(nTag);
-// newTagBeingEdited->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
-// newTagBeingEdited->setCheckState(Qt::Unchecked);
-// FMFontDb::DB()->addTagToDB ( nTag );
-// currentTag = nTag;
-
-
-// tagsListWidget->setFocus();
-// tagsListWidget->addItem(newTagBeingEdited);
-// tagsListWidget->scrollToItem(newTagBeingEdited);
-// tagsListWidget->setCurrentItem(newTagBeingEdited);
-// tagsListWidget->openPersistentEditor(newTagBeingEdited);
-
-// disconnect(tagsListWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(slotActEditTag(QListWidgetItem*)));
-// connect(tagsListWidget, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(slotActEditTag(QListWidgetItem*)));
+ QModelIndex idx(model->addTag());
+ if(!idx.isValid())
+ return;
+ tagsListView->setCurrentIndex(idx);
+ tagsListView->edit(idx);
}
void TagsWidget::slotActRemovetag()
{
-// if(currentTag.isEmpty())
-// return;
+ QModelIndex idx(tagsListView->currentIndex());
+ if(!idx.isValid())
+ return;
+ QString currentTag(model->data(idx, Qt::DisplayRole).toString());
QString message;
- message = tr ( "Please confirm that you want to remove\nthe following tag from database:" ) /*+ " " + currentTag*/;
+ message = tr ( "Please confirm that you want to remove\nthe following tag from database:" ) + " " + currentTag;
if ( QMessageBox::question ( typotek::getInstance(),
"Fontmatrix",
message ,
QMessageBox::Ok | QMessageBox::Cancel,
QMessageBox::Cancel )
== QMessageBox::Ok )
{
-// FMFontDb::DB()->removeTagFromDB ( currentTag );
+ FMFontDb::DB()->removeTagFromDB ( currentTag );
}
}
@@ -16,9 +16,15 @@
TagsWidget_ListModel::TagsWidget_ListModel(QObject *parent)
- :QAbstractListModel(parent)
+ :QAbstractListModel(parent),
+ newTagString(tr("New Tag"))
{
+ connect(FMFontDb::DB(), SIGNAL(tagsChanged()), this, SLOT(updateTags()));
+}
+void TagsWidget_ListModel::updateTags()
+{
+ emit dataChanged(index(0), index(rowCount() - 1));
}
int TagsWidget_ListModel::rowCount(const QModelIndex &parent) const
@@ -137,6 +143,28 @@ void TagsWidget_ListModel::setFonts(const QList<FontItem *> &flist)
fonts = flist;
if(fonts.count() > 0)
tags = fonts.first()->tags();
- emit dataChanged(index(0), index(rowCount() - 1));
+ updateTags();
+}
+
+
+QModelIndex TagsWidget_ListModel::addTag()
+{
+ if(FMFontDb::DB()->getTags().contains(newTagString))
+ return QModelIndex();
+
+ FMFontDb::DB()->addTagToDB(newTagString);
+ updateTags();
+
+ QStringList tl_tmp = FMFontDb::DB()->getTags();
+ tl_tmp.sort();
+
+ for(int i(0); i < tl_tmp.count(); ++i)
+ {
+ if(tl_tmp.at(i) == newTagString)
+ return index(i);
+ }
+
}
+
+
@@ -21,6 +21,10 @@ class TagsWidget_ListModel : public QAbstractListModel
QList<FontItem*> fonts;
QStringList tags;
+ const QString newTagString;
+
+private slots:
+ void updateTags();
public:
@@ -32,6 +36,7 @@ class TagsWidget_ListModel : public QAbstractListModel
Qt::ItemFlags flags ( const QModelIndex & index ) const;
void setFonts(const QList<FontItem*>& flist);
+ QModelIndex addTag();
};

0 comments on commit 696fca4

Please sign in to comment.