2012-05-29 13:31:11 +00:00
|
|
|
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-01-06 11:29:34 +00:00
|
|
|
#include "common.h"
|
2012-05-29 13:31:11 +00:00
|
|
|
#include <QtGui/QtGui>
|
|
|
|
#include "tiles_model.h"
|
|
|
|
#include "tile_widget.h"
|
|
|
|
|
|
|
|
|
|
|
|
bool caseInsensitiveLessThan(const TileModel &t1, const TileModel &t2)
|
|
|
|
{
|
|
|
|
return t1.getIndex() < t2.getIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TileModel::TileModel():pixmapSide(128),tileLabel(QObject::tr("Right-Click to select Bitmap")), index(-1)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TileModel::TileModel(const QPixmap &pixmap, QString tileLabel, int index):pixmap(pixmap), tileLabel(tileLabel), index(index)
|
|
|
|
{
|
|
|
|
pixmapSide = pixmap.width();
|
|
|
|
}
|
|
|
|
|
|
|
|
TileModel::TileModel(int pixmapSide, QString tileLabel, int index):pixmapSide(pixmapSide), index(index)
|
|
|
|
{
|
|
|
|
if (!tileLabel.isEmpty())
|
|
|
|
this->tileLabel = tileLabel;
|
|
|
|
else
|
|
|
|
this->tileLabel = QObject::tr("Right-Click to select Bitmap");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tiles_model::tiles_model(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant tiles_model::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2016-02-17 14:07:19 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
2012-05-29 13:31:11 +00:00
|
|
|
|
2016-02-17 14:07:19 +00:00
|
|
|
if (role == Qt::DecorationRole || role == Qt::UserRole)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-06 11:29:34 +00:00
|
|
|
CTile_Widget wiwi;
|
|
|
|
wiwi.initWidget(tiles.value(index.row()).getPixmap(), tiles.value(index.row()).getPixmapSide(), tiles.value(index.row()).getTileLabel());
|
2016-01-06 15:01:53 +00:00
|
|
|
#ifdef USE_QT5
|
2016-01-06 11:29:34 +00:00
|
|
|
QPixmap pixpix = wiwi.grab(wiwi.contentsRect());
|
2016-01-06 15:01:53 +00:00
|
|
|
#else
|
2016-02-17 14:03:57 +00:00
|
|
|
QPixmap pixpix = QPixmap::grabWidget(&wiwi, wiwi.contentsRect());
|
2016-01-06 15:01:53 +00:00
|
|
|
#endif
|
2012-05-29 13:31:11 +00:00
|
|
|
return pixpix;
|
|
|
|
}
|
2016-02-17 14:07:19 +00:00
|
|
|
else if (role == Qt::UserRole + 1)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-02-17 14:07:19 +00:00
|
|
|
return tiles.value(index.row()).getIndex();
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:07:19 +00:00
|
|
|
return QVariant();
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tiles_model::sort ( int column, Qt::SortOrder order)
|
|
|
|
{
|
|
|
|
qSort(tiles.begin(), tiles.end(), caseInsensitiveLessThan);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tiles_model::addTile(const TileModel &tile)
|
|
|
|
{
|
|
|
|
|
|
|
|
int row;
|
|
|
|
if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1)
|
|
|
|
row = 0;
|
|
|
|
else
|
|
|
|
row = tiles.size();
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), row, row);
|
|
|
|
|
|
|
|
tiles.insert(row, tile);
|
|
|
|
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Qt::ItemFlags tiles_model::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (index.isValid())
|
|
|
|
return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
|
|
|
|
|
|
|
|
return Qt::ItemIsDropEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool tiles_model::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!tiles.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (row >= tiles.size() || row + count <= 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int beginRow = qMax(0, row);
|
|
|
|
int endRow = qMin(row + count - 1, tiles.size() - 1);
|
|
|
|
|
|
|
|
beginRemoveRows(parent, beginRow, endRow);
|
|
|
|
while (beginRow <= endRow) {
|
|
|
|
tiles.removeAt(beginRow);
|
|
|
|
++beginRow;
|
|
|
|
}
|
|
|
|
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int tiles_model::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return tiles.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void tiles_model::removeAllTiles()
|
|
|
|
{
|
|
|
|
if(tiles.size())
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), 0, tiles.size() - 1);
|
|
|
|
tiles.clear();
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
2010-05-06 00:08:41 +00:00
|
|
|
}
|