From 471cf26b1175ed967cc2f5897926f9e3e8de4f85 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Sat, 12 Jan 2008 02:31:32 +0000 Subject: [PATCH] New files, anticipating a later commit. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@22500 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/frontends/qt4/GuiIdListModel.cpp | 146 +++++++++++++++++++++++++++ src/frontends/qt4/GuiIdListModel.h | 127 +++++++++++++++++++++++ 2 files changed, 273 insertions(+) create mode 100644 src/frontends/qt4/GuiIdListModel.cpp create mode 100644 src/frontends/qt4/GuiIdListModel.h diff --git a/src/frontends/qt4/GuiIdListModel.cpp b/src/frontends/qt4/GuiIdListModel.cpp new file mode 100644 index 0000000000..807235297a --- /dev/null +++ b/src/frontends/qt4/GuiIdListModel.cpp @@ -0,0 +1,146 @@ +/** + * \file GuiIdListModel.cpp + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Richard Heck + * + * Some of this code is based upon qstringlistmodel.{h,cpp}, which is + * part of the Qt toolkit, copyright (C) 1992-2007 Trolltech ASA, and + * released under the General Public License. + * + * Full author contact details are available in file CREDITS. + */ + +#include + +#include "GuiIdListModel.h" + +using std::vector; + +namespace lyx { +namespace frontend { + +GuiIdListModel::GuiIdListModel() : + QAbstractListModel() +{} + + +QVariant GuiIdListModel::data(QModelIndex const & index, int role) const +{ + int const row = index.row(); + if (!rowIsValid(row)) + return QVariant(); + if (role == Qt::DisplayRole || role == Qt::EditRole) + return userData_[row].uiString; + if (role == Qt::UserRole) + return userData_[row].idString; + return QVariant(); +} + + +bool GuiIdListModel::setData (QModelIndex const & index, + const QVariant & value, int role) +{ + int const row = index.row(); + if (!rowIsValid(row)) + return false; + if (role == Qt::DisplayRole || role == Qt::EditRole) { + userData_[row].uiString = value; + dataChanged(index, index); + return true; + } + if (role == Qt::UserRole) { + userData_[row].idString = value; + dataChanged(index, index); + return true; + } + return false; +} + + +bool GuiIdListModel::insertRows(int row, int count, + QModelIndex const & /*parent*/) +{ + if (!rowIsValid(row)) + return false; + vector::iterator it = userData_.begin() + row; + OurData const v; + beginInsertRows(QModelIndex(), row, row + count - 1); + userData_.insert(it, count, v); + endInsertRows(); + return true; +} + + +bool GuiIdListModel::removeRows(int row, int count, + QModelIndex const & /*parent*/) +{ + if (!rowIsValid(row) || row + count > userData_.size() || + count < 0) + return false; + if (count == 0) + return true; + vector::iterator it = userData_.begin() + row; + beginRemoveRows(QModelIndex(), row, row + count - 1); + userData_.erase(it, it + count); + endRemoveRows(); + return true; +} + + +void GuiIdListModel::insertRow(int const i, std::string const & uiString, + std::string const & idString) +{ + insertRows(i, 1); + setUIString(i, uiString); + setIDString(i, idString); +} + + +QMap + GuiIdListModel::itemData(QModelIndex const & index ) const +{ + int const row = index.row(); + if (!rowIsValid(row)) + return QMap(); + QMap qm = QAbstractListModel::itemData(index); + qm[Qt::UserRole] = userData_[row].idString; + return qm; +} + +/* The following functions are currently unused but are retained here in + case they should at some point be useful. + +QStringList GuiIdListModel::getIDStringList() const { + QStringList qsl; + vector::const_iterator it = userData_.begin(); + vector::const_iterator end = userData_.end(); + for (; it != end; ++it) + qsl.append(it->idString.toString()); + return qsl; +} + + +void GuiIdListModel::insertRow(int const i, QString const & uiString, + QString const & idString) +{ + insertRows(i, 1); + setUIString(i, uiString); + setIDString(i, idString); +} + +bool GuiIdListModel::containsID(QVariant const & q) const +{ + vector::const_iterator it = userData_.begin(); + vector::const_iterator end = userData_.end(); + for (; it != end; ++it) + if (it->idString == q) + return true; + return false; +} +*/ + +} +} + diff --git a/src/frontends/qt4/GuiIdListModel.h b/src/frontends/qt4/GuiIdListModel.h new file mode 100644 index 0000000000..48709762e9 --- /dev/null +++ b/src/frontends/qt4/GuiIdListModel.h @@ -0,0 +1,127 @@ +// -*- C++ -*- +/** + * \file GuiIdListModel.h + * This file is part of LyX, the document processor. + * Licence details can be found in the file COPYING. + * + * \author Richard Heck + * + * Some of this code is based upon qstringlistmodel.{h,cpp}, which is + * part of the Qt toolkit, copyright (C) 1992-2007 Trolltech ASA, and + * released under the General Public License. + * + * Full author contact details are available in file CREDITS. + */ + +#ifndef GUIIDLISTMODEL_H +#define GUIIDLISTMODEL_H + +#include +#include +#include + +#include "support/qstring_helpers.h" + +namespace lyx { +namespace frontend { + +/** + * A QAbstractListModel that associates an identifying string with + * each item, as well as a display string. The display string is set + * with setUIString; the identifying string, with setIDString. + * + * This is intended to be used, for example, with GuiSelectionManager. + * In that case, one needs to recover from selectedModel which items + * have been selected. One may not wish to do so using the string that + * is there *displayed*, since, among other things, that string may be + * translated. So the id can be used to identify the items in this case. + */ +class GuiIdListModel : public QAbstractListModel { +public: + /// + explicit GuiIdListModel(); + ////////////////////////////////////////////////////////////////////// + // Methods overridden from QAbstractListModel + ////////////////////////////////////////////////////////////////////// + /// + inline int rowCount(QModelIndex const & parent = QModelIndex()) const; + /// + virtual QVariant data(QModelIndex const & index, + int role = Qt::DisplayRole) const; + /// + bool insertRows(int row, int count, QModelIndex const & parent = QModelIndex()); + /// + bool removeRows(int row, int count, QModelIndex const & parent = QModelIndex()); + /// + void clear() { removeRows(0, rowCount()); }; + /// + virtual bool setData (QModelIndex const & index, + const QVariant & value, int role = Qt::EditRole ); + /// + virtual QMap itemData(QModelIndex const & index ) const; + ////////////////////////////////////////////////////////////////////// + // New methods + ////////////////////////////////////////////////////////////////////// + /// + inline void setUIString(QModelIndex const & index, QString const & value) + { setData(index, value); }; + /// + inline void setUIString(int const i, std::string const & value) + { setUIString(index(i), toqstr(value)); }; + /// + inline void setIDString(QModelIndex const & index, QString const & value) + { setData(index, value, Qt::UserRole); }; + /// + inline void setIDString(int const i, std::string const & value) + { setIDString(index(i), toqstr(value)); }; + /// + virtual QString getIDString(QModelIndex const & index) const + { return data(index, Qt::UserRole).toString(); }; + /// + virtual std::string getIDString(int const i) const + { return fromqstr(getIDString(index(i))); }; + /// + void insertRow(int const i, std::string const & uiString, + std::string const & idString); + /* The following functions are currently unused but are retained here in + case they should at some point be useful. + /// + void setUIString(int const i, QString const & value) + { setUIString(index(i), value); }; + /// + void setIDString(int const i, QString const & value) + { setIDString(index(i), value); }; + /// + QStringList getIDStringList() const; + /// + void insertRow(int const i, QString const & uiString, + QString const & idString); + /// Returns whether the model contains an item with the given ID + bool containsID(QVariant const &) const; + */ +private: + /// noncopyable + GuiIdListModel(GuiIdListModel const &); + /// + struct OurData { + QVariant uiString; + QVariant idString; + }; + /// + std::vector userData_; + /// + inline bool rowIsValid(int const i) const + { + return (i >= 0 && (i <= userData_.size())); + } +; +}; + + +//definition is here to silence a warning +inline int GuiIdListModel::rowCount(QModelIndex const &) const + { return userData_.size(); } + +} +} +#endif //GUIIDLISTMODEL_H