2008-01-12 02:31:32 +00:00
|
|
|
// -*- 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
|
|
|
|
|
2008-01-12 16:11:55 +00:00
|
|
|
#include "support/qstring_helpers.h"
|
|
|
|
|
2008-01-12 02:31:32 +00:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
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:
|
|
|
|
///
|
2008-01-19 12:33:01 +00:00
|
|
|
GuiIdListModel() {}
|
2008-01-12 02:31:32 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods overridden from QAbstractListModel
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
2008-02-05 15:16:20 +00:00
|
|
|
int rowCount(QModelIndex const & = QModelIndex()) const
|
2008-01-19 12:33:01 +00:00
|
|
|
{ return userData_.size(); }
|
|
|
|
|
2008-01-12 02:31:32 +00:00
|
|
|
///
|
|
|
|
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());
|
|
|
|
///
|
2008-01-19 12:33:01 +00:00
|
|
|
void clear() { removeRows(0, rowCount()); }
|
2008-01-12 02:31:32 +00:00
|
|
|
///
|
|
|
|
virtual bool setData (QModelIndex const & index,
|
|
|
|
const QVariant & value, int role = Qt::EditRole );
|
|
|
|
///
|
|
|
|
virtual QMap<int, QVariant> itemData(QModelIndex const & index ) const;
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// New methods
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
2008-04-17 02:43:57 +00:00
|
|
|
void insertRow(int const i, QString const & uiString,
|
|
|
|
std::string const & idString, QString const & ttString);
|
|
|
|
/// A convenience method, setting ttString to the same as uiString
|
2008-01-14 21:44:06 +00:00
|
|
|
void insertRow(int const i, QString const & uiString,
|
2008-01-12 02:31:32 +00:00
|
|
|
std::string const & idString);
|
2008-02-28 21:04:55 +00:00
|
|
|
/// \return the index of the (first) item with idString
|
|
|
|
/// \return -1 if not found
|
|
|
|
int findIDString(std::string const & idString);
|
2008-01-12 02:31:32 +00:00
|
|
|
///
|
2008-04-17 02:43:57 +00:00
|
|
|
virtual QString getIDString(QModelIndex const & index) const
|
|
|
|
{ return data(index, Qt::UserRole).toString(); }
|
2008-01-14 21:44:06 +00:00
|
|
|
///
|
2008-04-17 02:43:57 +00:00
|
|
|
virtual std::string getIDString(int const i) const
|
|
|
|
{ return fromqstr(getIDString(index(i))); }
|
|
|
|
|
2008-01-12 02:31:32 +00:00
|
|
|
private:
|
|
|
|
/// noncopyable
|
|
|
|
GuiIdListModel(GuiIdListModel const &);
|
2008-02-24 22:40:34 +00:00
|
|
|
///
|
2008-01-19 12:33:01 +00:00
|
|
|
void operator=(GuiIdListModel const &);
|
2008-01-12 02:31:32 +00:00
|
|
|
///
|
2008-04-17 02:43:57 +00:00
|
|
|
void setUIString(QModelIndex const & index, QString const & value)
|
|
|
|
{ setData(index, value); }
|
|
|
|
///
|
|
|
|
void setUIString(int const i, QString const & value)
|
|
|
|
{ setUIString(index(i), value); }
|
|
|
|
///
|
|
|
|
void setIDString(QModelIndex const & index, QString const & value)
|
|
|
|
{ setData(index, value, Qt::UserRole); }
|
|
|
|
///
|
|
|
|
void setIDString(int const i, std::string const & value)
|
|
|
|
{ setIDString(index(i), toqstr(value)); }
|
|
|
|
///
|
|
|
|
void setTTString(QModelIndex const & index, QString const & value)
|
|
|
|
{ setData(index, value, Qt::ToolTipRole); }
|
|
|
|
///
|
|
|
|
void setTTString(int const i, QString const & value)
|
|
|
|
{ setTTString(index(i), value); }
|
2008-01-12 02:31:32 +00:00
|
|
|
struct OurData {
|
2008-04-17 02:43:57 +00:00
|
|
|
/// Qt::DisplayRole and Qt::EditRole
|
2008-01-12 02:31:32 +00:00
|
|
|
QVariant uiString;
|
2008-04-17 02:43:57 +00:00
|
|
|
/// Qt::UserRole
|
2008-01-12 02:31:32 +00:00
|
|
|
QVariant idString;
|
2008-04-17 02:43:57 +00:00
|
|
|
/// Qt::ToolTipRole
|
|
|
|
QVariant ttString;
|
2008-01-12 02:31:32 +00:00
|
|
|
};
|
|
|
|
///
|
|
|
|
std::vector<OurData> userData_;
|
|
|
|
///
|
2008-01-19 12:33:01 +00:00
|
|
|
bool rowIsValid(int const i) const
|
2008-01-12 02:31:32 +00:00
|
|
|
{
|
2008-01-13 10:44:16 +00:00
|
|
|
return i >= 0 && i <= int(userData_.size());
|
2008-01-12 02:31:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif //GUIIDLISTMODEL_H
|