1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
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.
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/>.
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
;
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
59 if (role
== Qt::DecorationRole
|| role
== Qt::UserRole
)
62 wiwi
.initWidget(tiles
.value(index
.row()).getPixmap(), tiles
.value(index
.row()).getPixmapSide(), tiles
.value(index
.row()).getTileLabel());
64 QPixmap pixpix
= wiwi
.grab(wiwi
.contentsRect());
66 QPixmap pixpix
= QPixmap::grabWidget(&wiwi
, wiwi
.contentsRect());
70 else if (role
== Qt::UserRole
+ 1)
72 return tiles
.value(index
.row()).getIndex();
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
)
88 if (int(2.0*qrand()/(RAND_MAX
+1.0)) == 1)
93 beginInsertRows(QModelIndex(), row
, row
);
95 tiles
.insert(row
, tile
);
101 Qt::ItemFlags
tiles_model::flags(const QModelIndex
&index
) const
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())
117 if (row
>= tiles
.size() || row
+ count
<= 0)
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
);
135 int tiles_model::rowCount(const QModelIndex
&parent
) const
137 if (parent
.isValid())
144 void tiles_model::removeAllTiles()
148 beginRemoveRows(QModelIndex(), 0, tiles
.size() - 1);