[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: Re: An Useful Model Proposal : KStringDataListModel
From: Bruno Virlet <bruno.virlet () gmail ! com>
Date: 2007-03-02 11:36:30
Message-ID: 200703021236.32363.bruno.virlet () gmail ! com
[Download RAW message or body]
Hello !
On Thursday 01 March 2007, David Faure wrote:
> > At the risk of asking the obvious: Why not simply use a
> > QStandardItemModel?
> > [...]
> Hmpf. I admit that I didn't think of doing it that way.
I ported my model to use QStandardItemModel. It is actually nicer.
Please find the patch attached.
Bruno
["kstringdatalistmodel.patch" (text/x-diff)]
Index: itemviews/kstringdatalistmodel.cpp
===================================================================
--- itemviews/kstringdatalistmodel.cpp (revision 0)
+++ itemviews/kstringdatalistmodel.cpp (revision 0)
@@ -0,0 +1,99 @@
+/* This file is part of the KDE libraries
+ Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+#include "kstringdatalistmodel.h"
+
+#include <QStringList>
+
+KStringDataListModel::KStringDataListModel(QObject *parent)
+ : QStandardItemModel(parent)
+{
+}
+
+KStringDataListModel::~KStringDataListModel()
+{
+}
+
+void KStringDataListModel::append(const QList<Pair>& pairList)
+{
+ foreach( Pair pair, pairList) {
+ QStandardItem *i = new QStandardItem( pair.first );
+ i->setData( pair.second );
+ appendRow( i );
+ }
+
+}
+
+QString KStringDataListModel::label(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QString();
+
+ return itemFromIndex( index )->text();
+}
+
+
+QVariant KStringDataListModel::internal(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ return itemFromIndex( index )->data();
+}
+
+QModelIndex KStringDataListModel::insertRow( const QString &label, const QVariant \
&internalData, const QModelIndex &index) +{
+ QStandardItem *item = new QStandardItem(label);
+ item->setData(internalData);
+
+ if (index.isValid())
+ {
+ beginInsertRows(QModelIndex(), index.row() + 1, index.row() + 1);
+ QStandardItemModel::insertRow( index.row() + 1, item);
+ endInsertRows();
+ }
+ else
+ appendRow( i );
+
+ return indexFromItem(i);
+}
+
+void KStringDataListModel::moveUp( const QModelIndex &index )
+{
+ if (!index.isValid() || index.row() == 0)
+ return;
+
+ emit layoutAboutToBeChanged();
+ QList<QStandardItem*> items = takeRow(index.row() - 1);
+ QStandardItemModel::insertRow( index.row(), items );
+ emit layoutChanged();
+}
+
+void KStringDataListModel::moveDown( const QModelIndex &index )
+{
+ if (!index.isValid() || index.row() == rowCount() - 1 )
+ return;
+
+ emit layoutAboutToBeChanged();
+ QList<QStandardItem*> items = takeRow(index.row() + 1);
+ QStandardItemModel::insertRow( index.row(), items );
+ emit layoutChanged();
+}
+
+
+#include "kstringdatalistmodel.moc"
Index: itemviews/kstringdatalistmodel.h
===================================================================
--- itemviews/kstringdatalistmodel.h (revision 0)
+++ itemviews/kstringdatalistmodel.h (revision 0)
@@ -0,0 +1,88 @@
+/* This file is part of the KDE libraries
+ Copyright (c) 2007 Bruno Virlet <bruno.virlet@gmail.com>
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License version 2 as published by the Free Software Foundation.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public License
+ along with this library; see the file COPYING.LIB. If not, write to
+ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+
+
+#ifndef KSTRINGDATALISTMODEL_H
+#define KSTRINGDATALISTMODEL_H
+
+#include <QStandardItemModel>
+#include <QList>
+#include <QPair>
+
+#include <kdelibs_export.h>
+
+class QVariant;
+class QString;
+
+/**
+ * Model containing a list of pairs of QString/QVariant.
+ *
+ * It is useful when you need to show a string to the user
+ * and use another one internally for instance.
+ *
+ * It also implements methods to move elements up and down in the list.
+ */
+class KDEUI_EXPORT KStringDataListModel : public QStandardItemModel
+{
+ Q_OBJECT
+
+ typedef QPair<QString, QVariant> Pair;
+
+public:
+ KStringDataListModel( QObject *parent = 0 );
+ ~KStringDataListModel();
+
+ /**
+ * Inserts a row with @param label as visual label and @param internalString as \
internal + * parameter after the row given by @param index. If @param index is \
null, then it inserts it at + * the end.
+ *
+ * Returns the index of the newly inserted item.
+ */
+ QModelIndex insertRow( const QString &label, const QVariant &internalString, \
const QModelIndex &index ); +
+ /**
+ * Moves the element given by @param index up in the list
+ * If @param index is invalid or if the element is the first one,
+ * it does nothing.
+ */
+ void moveUp( const QModelIndex &index );
+ /**
+ * Moves the element given by @param index down in the list
+ * If @param index is invalid or if the element is the last one,
+ * it does nothing.
+ */
+ void moveDown( const QModelIndex &index );
+
+ /**
+ * Retrieve the internal QVariant of index @param index
+ */
+ QVariant internal( const QModelIndex &index ) const;
+ /**
+ * Retrieve the label of index @param index
+ */
+ QString label( const QModelIndex &index ) const;
+
+ /**
+ * Appends the pairs of QString/QVariant data to the model
+ */
+ void append( const QList<Pair>& pairList );
+};
+
+#endif
+
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt (revision 637392)
+++ CMakeLists.txt (working copy)
@@ -83,6 +83,7 @@
itemviews/k3listviewsearchline.cpp
itemviews/klistwidget.cpp
itemviews/ktreewidgetsearchline.cpp
+ itemviews/kstringdatalistmodel.cpp
kernel/kapplication.cpp
kernel/kclipboard.cpp
kernel/kuniqueapplication.cpp
@@ -278,6 +279,7 @@
itemviews/k3listviewsearchline.h
itemviews/klistwidget.h
itemviews/ktreewidgetsearchline.h
+ itemviews/kstringdatalistmodel.h
kernel/kapplication.h
kernel/kuniqueapplication.h
kernel/ksessionmanager.h
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic