[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-core-devel
Subject: An Useful Model Proposal : KStringDataListModel
From: "Bruno Virlet" <bruno.virlet () gmail ! com>
Date: 2007-02-28 18:04:07
Message-ID: 5375fad50702281004u5e2198c7sd0b07df27fd7fb2a () mail ! gmail ! com
[Download RAW message or body]
Hello,
Willing to rewrite the tooltip editor of Kopete, I have been lacking a
model managing pairs of strings : one to be displayed (the label) and
another to be kept for internal use.
David Faure suggested that it could be useful in many places, so I
wrote a model managing a pair with a QString and a QVariant, in a more
general design.
Please find attached the files. I am awaiting your critics ;-)
Regards,
Bruno
["kstringdatalistmodel.patch" (application/octet-stream)]
Index: itemviews/kstringdatalistmodel.cpp
===================================================================
--- itemviews/kstringdatalistmodel.cpp (revision 0)
+++ itemviews/kstringdatalistmodel.cpp (revision 0)
@@ -0,0 +1,132 @@
+/* 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>
+#include <QMimeData>
+
+#include "kdebug.h"
+
+KStringDataListModel::KStringDataListModel(QObject *parent)
+ : QAbstractListModel(parent)
+{
+ mList = new QList<Pair>();
+}
+
+KStringDataListModel::~KStringDataListModel()
+{
+ delete mList;
+}
+
+void KStringDataListModel::append(const QList<Pair>& pairList)
+{
+ *mList += pairList;
+}
+
+QVariant KStringDataListModel::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (role == Qt::DisplayRole)
+ return mList->value(index.row()).first;
+
+ return QVariant();
+}
+
+int KStringDataListModel::rowCount(const QModelIndex &parent) const
+{
+ if (parent.isValid())
+ return 0;
+ else
+ return mList->size();
+}
+
+QModelIndex KStringDataListModel::index(int row) const
+{
+ if (row < 0 || row > mList->count())
+ return QModelIndex();
+
+ return createIndex(row, 0);
+}
+
+QString KStringDataListModel::label(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QString();
+
+ return mList->value(index.row()).first;
+}
+
+
+QVariant KStringDataListModel::internal(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ return mList->value(index.row()).second;
+}
+
+bool KStringDataListModel::removeRow( const QModelIndex &index )
+{
+ if (!index.isValid())
+ return false;
+
+ beginRemoveRows(QModelIndex(), index.row(), index.row());
+ mList->removeAt(index.row());
+ endRemoveRows();
+
+ return true;
+}
+
+bool KStringDataListModel::insertRow( const QString label, const QVariant \
internalString, const QModelIndex &index) +{
+ int row = 0;
+ if (index.isValid())
+ row = index.row();
+
+ beginInsertRows(QModelIndex(), row, row);
+ mList->insert(row, qMakePair(label, internalString));
+ endInsertRows();
+
+ return true;
+}
+
+void KStringDataListModel::moveUp( const QModelIndex &index )
+{
+ if (!index.isValid() || index.row() == 0)
+ return;
+
+ emit layoutAboutToBeChanged();
+ mList->move(index.row(), index.row() - 1);
+ emit layoutChanged();
+}
+
+void KStringDataListModel::moveDown( const QModelIndex &index )
+{
+ if (!index.isValid() || index.row() == mList->count() - 1 )
+ return;
+
+ emit layoutAboutToBeChanged();
+ mList->move(index.row(), index.row() + 1);
+ emit layoutChanged();
+}
+
+
+#include "kstringdatalistmodel.moc"
Index: itemviews/kstringdatalistmodel.h
===================================================================
--- itemviews/kstringdatalistmodel.h (revision 0)
+++ itemviews/kstringdatalistmodel.h (revision 0)
@@ -0,0 +1,121 @@
+/* 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 <QAbstractListModel>
+#include <QList>
+#include <QPair>
+
+#include <kdelibs_export.h>
+
+class QVariant;
+class QString;
+class QStringList;
+class QMimeData;
+
+typedef QPair<QString, QVariant> Pair;
+
+/**
+ * Model containing a List with pairs of QString and 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 QAbstractListModel
+{
+ Q_OBJECT
+
+public:
+ KStringDataListModel( QObject *parent = 0 );
+ ~KStringDataListModel();
+
+ /**
+ * Only for view usage. Preferably use @method label and @method internal
+ *
+ * Returns the label if @param role is Qt::DisplayRole.
+ */
+ QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
+
+ /**
+ * Returns the number of lines in the list
+ */
+ int rowCount( const QModelIndex &parent = QModelIndex() ) const;
+
+ /**
+ * Returns the index of the element at row @param row in the list
+ */
+ QModelIndex index(int row) const;
+
+
+ /////////////////////////////////////
+ // KStringDataListModel specific API
+
+ /**
+ * 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 true if success.
+ */
+ bool insertRow( const QString label, const QVariant internalString, const \
QModelIndex &index ); +
+ /**
+ * Deletes the row given by @param index
+ *
+ * Returns true if success.
+ */
+ bool removeRow( 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 data to the model
+ */
+ void append( const QList<Pair>& pairList );
+
+ private:
+ QList<Pair> *mList;
+};
+
+#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