Linux multi-monitor fullscreen support
[ryzomcore.git] / nel / tools / 3d / tile_edit_qt / tiles_model.cpp
blobcabd810d7b7b7b2166466c1b78e1adedb600cc18
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "common.h"
18 #include <QtGui/QtGui>
19 #include "tiles_model.h"
20 #include "tile_widget.h"
23 bool caseInsensitiveLessThan(const TileModel &t1, const TileModel &t2)
25 return t1.getIndex() < t2.getIndex();
29 TileModel::TileModel():pixmapSide(128),tileLabel(QObject::tr("Right-Click to select Bitmap")), index(-1)
33 TileModel::TileModel(const QPixmap &pixmap, QString tileLabel, int index):pixmap(pixmap), tileLabel(tileLabel), index(index)
35 pixmapSide = pixmap.width();
38 TileModel::TileModel(int pixmapSide, QString tileLabel, int index):pixmapSide(pixmapSide), index(index)
40 if (!tileLabel.isEmpty())
41 this->tileLabel = tileLabel;
42 else
43 this->tileLabel = QObject::tr("Right-Click to select Bitmap");
49 tiles_model::tiles_model(QObject *parent)
50 : QAbstractListModel(parent)
54 QVariant tiles_model::data(const QModelIndex &index, int role) const
56 if (!index.isValid())
57 return QVariant();
59 if (role == Qt::DecorationRole || role == Qt::UserRole)
61 CTile_Widget wiwi;
62 wiwi.initWidget(tiles.value(index.row()).getPixmap(), tiles.value(index.row()).getPixmapSide(), tiles.value(index.row()).getTileLabel());
63 #ifdef USE_QT5
64 QPixmap pixpix = wiwi.grab(wiwi.contentsRect());
65 #else
66 QPixmap pixpix = QPixmap::grabWidget(&wiwi, wiwi.contentsRect());
67 #endif
68 return pixpix;
70 else if (role == Qt::UserRole + 1)
72 return tiles.value(index.row()).getIndex();
75 return QVariant();
78 void tiles_model::sort ( int column, Qt::SortOrder order)
80 qSort(tiles.begin(), tiles.end(), caseInsensitiveLessThan);
84 void tiles_model::addTile(const TileModel &tile)
87 int row;
88 if (int(2.0*qrand()/(RAND_MAX+1.0)) == 1)
89 row = 0;
90 else
91 row = tiles.size();
93 beginInsertRows(QModelIndex(), row, row);
95 tiles.insert(row, tile);
97 endInsertRows();
101 Qt::ItemFlags tiles_model::flags(const QModelIndex &index) const
103 if (index.isValid())
104 return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
106 return Qt::ItemIsDropEnabled;
109 bool tiles_model::removeRows(int row, int count, const QModelIndex &parent)
111 if (parent.isValid())
112 return false;
114 if(!tiles.size())
115 return false;
117 if (row >= tiles.size() || row + count <= 0)
118 return false;
120 int beginRow = qMax(0, row);
121 int endRow = qMin(row + count - 1, tiles.size() - 1);
123 beginRemoveRows(parent, beginRow, endRow);
124 while (beginRow <= endRow) {
125 tiles.removeAt(beginRow);
126 ++beginRow;
129 endRemoveRows();
131 return true;
135 int tiles_model::rowCount(const QModelIndex &parent) const
137 if (parent.isValid())
138 return 0;
139 else
140 return tiles.size();
144 void tiles_model::removeAllTiles()
146 if(tiles.size())
148 beginRemoveRows(QModelIndex(), 0, tiles.size() - 1);
149 tiles.clear();
150 endRemoveRows();