2008-01-12 02:31:32 +00:00
|
|
|
/**
|
|
|
|
* \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 <config.h>
|
|
|
|
|
|
|
|
#include "GuiIdListModel.h"
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2008-04-17 13:15:27 +00:00
|
|
|
|
2008-01-12 02:31:32 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2008-04-17 02:43:57 +00:00
|
|
|
|
|
|
|
// Note: Any role that is added here must also be added to setData().
|
2008-01-12 02:31:32 +00:00
|
|
|
QVariant GuiIdListModel::data(QModelIndex const & index, int role) const
|
|
|
|
{
|
|
|
|
int const row = index.row();
|
|
|
|
if (!rowIsValid(row))
|
|
|
|
return QVariant();
|
2008-04-17 02:43:57 +00:00
|
|
|
if (role == Qt::DisplayRole || role == Qt::EditRole)
|
2008-01-12 02:31:32 +00:00
|
|
|
return userData_[row].uiString;
|
2008-04-17 02:43:57 +00:00
|
|
|
if (role == Qt::ToolTipRole) {
|
|
|
|
QString const ttstr = userData_[row].ttString.toString();
|
|
|
|
return !ttstr.isEmpty() ? ttstr : userData_[row].uiString;
|
|
|
|
}
|
2008-01-12 02:31:32 +00:00
|
|
|
if (role == Qt::UserRole)
|
|
|
|
return userData_[row].idString;
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
bool GuiIdListModel::setData (QModelIndex const & index,
|
2008-01-12 02:31:32 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-04-17 02:43:57 +00:00
|
|
|
if (role == Qt::ToolTipRole) {
|
|
|
|
userData_[row].ttString = value;
|
|
|
|
dataChanged(index, index);
|
|
|
|
return true;
|
|
|
|
}
|
2008-04-17 13:15:27 +00:00
|
|
|
// If we assert here, it's because we're trying to set an
|
|
|
|
// unrecognized role.
|
2013-04-25 21:27:10 +00:00
|
|
|
LATTEST(false);
|
|
|
|
return false;
|
2008-01-12 02:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
bool GuiIdListModel::insertRows(int row, int count,
|
2008-01-12 02:31:32 +00:00
|
|
|
QModelIndex const & /*parent*/)
|
|
|
|
{
|
|
|
|
if (!rowIsValid(row))
|
|
|
|
return false;
|
|
|
|
vector<OurData>::iterator it = userData_.begin() + row;
|
|
|
|
beginInsertRows(QModelIndex(), row, row + count - 1);
|
2011-02-07 22:47:02 +00:00
|
|
|
userData_.insert(it, count, OurData());
|
2008-01-12 02:31:32 +00:00
|
|
|
endInsertRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
bool GuiIdListModel::removeRows(int row, int count,
|
2008-01-12 02:31:32 +00:00
|
|
|
QModelIndex const & /*parent*/)
|
|
|
|
{
|
2008-01-13 10:48:10 +00:00
|
|
|
if (!rowIsValid(row) || row + count > int(userData_.size()) ||
|
2008-01-12 02:31:32 +00:00
|
|
|
count < 0)
|
|
|
|
return false;
|
|
|
|
if (count == 0)
|
|
|
|
return true;
|
|
|
|
vector<OurData>::iterator it = userData_.begin() + row;
|
|
|
|
beginRemoveRows(QModelIndex(), row, row + count - 1);
|
2017-07-03 17:53:14 +00:00
|
|
|
userData_.erase(it, it + count);
|
2008-01-12 02:31:32 +00:00
|
|
|
endRemoveRows();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
void GuiIdListModel::insertRow(int const i, QString const & uiString,
|
2008-01-12 02:31:32 +00:00
|
|
|
std::string const & idString)
|
2008-04-17 02:43:57 +00:00
|
|
|
{
|
|
|
|
insertRow(i, uiString, idString, uiString);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
void GuiIdListModel::insertRow(int const i, QString const & uiString,
|
2008-04-17 02:43:57 +00:00
|
|
|
std::string const & idString, QString const & ttString)
|
2008-01-12 02:31:32 +00:00
|
|
|
{
|
|
|
|
insertRows(i, 1);
|
|
|
|
setUIString(i, uiString);
|
|
|
|
setIDString(i, idString);
|
2008-04-17 02:43:57 +00:00
|
|
|
setTTString(i, ttString);
|
2008-01-12 02:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-28 21:04:55 +00:00
|
|
|
QMap<int, QVariant> GuiIdListModel::itemData(QModelIndex const & index) const
|
2008-01-12 02:31:32 +00:00
|
|
|
{
|
|
|
|
int const row = index.row();
|
|
|
|
if (!rowIsValid(row))
|
|
|
|
return QMap<int, QVariant>();
|
|
|
|
QMap<int, QVariant> qm = QAbstractListModel::itemData(index);
|
|
|
|
qm[Qt::UserRole] = userData_[row].idString;
|
|
|
|
return qm;
|
|
|
|
}
|
|
|
|
|
2008-02-28 21:04:55 +00:00
|
|
|
|
2017-07-03 17:53:14 +00:00
|
|
|
int GuiIdListModel::findIDString(std::string const & idString)
|
2008-02-28 21:04:55 +00:00
|
|
|
{
|
|
|
|
vector<OurData>::const_iterator it = userData_.begin();
|
|
|
|
vector<OurData>::const_iterator end = userData_.end();
|
|
|
|
for (; it != end; ++it)
|
|
|
|
if (fromqstr(it->idString.toString()) == idString)
|
|
|
|
return it - userData_.begin();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-01-19 12:33:01 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
2008-01-12 02:31:32 +00:00
|
|
|
|