@@ -39,23 +39,24 @@ WangColorModel::WangColorModel(TilesetDocument *tilesetDocument,
3939 : QAbstractListModel(parent)
4040 , mTilesetDocument(tilesetDocument)
4141 , mWangSet(wangSet)
42+ , mEraserIcon(QLatin1String(" :images/22/stock-tool-eraser.png" ))
4243{
4344}
4445
45- QModelIndex WangColorModel::colorIndex (int color) const
46+ QModelIndex WangColorModel::colorModelIndex (int color) const
4647{
47- if (!mWangSet || color > mWangSet ->colorCount ())
48+ if (!mWangSet || color < 0 || color > mWangSet ->colorCount ())
4849 return QModelIndex ();
4950
50- return createIndex (color - 1 , 0 );
51+ return createIndex (color, 0 );
5152}
5253
5354int WangColorModel::rowCount (const QModelIndex &parent) const
5455{
5556 if (!mWangSet || parent.isValid ())
5657 return 0 ;
5758
58- return mWangSet ->colorCount ();
59+ return mWangSet ->colorCount () + 1 ;
5960}
6061
6162int WangColorModel::columnCount (const QModelIndex &parent) const
@@ -68,15 +69,25 @@ QVariant WangColorModel::data(const QModelIndex &index, int role) const
6869 if (!mWangSet )
6970 return QVariant ();
7071
72+ const int colorIndex = index.row ();
73+
7174 switch (role) {
75+ case WangColorIndexRole:
76+ return colorIndex;
7277 case Qt::DisplayRole:
7378 case Qt::EditRole:
79+ if (colorIndex == 0 )
80+ return QCoreApplication::translate (" Tiled::WangDock" , " Erase Terrain" );
7481 return wangColorAt (index)->name ();
7582 case Qt::DecorationRole:
76- if (Tile *tile = mWangSet ->tileset ()->findTile (wangColorAt (index)->imageId ()))
83+ if (colorIndex == 0 )
84+ return mEraserIcon ;
85+ if (Tile *tile = mWangSet ->tileset ()->findTile (wangColorAt (index)->imageId ()))
7786 return tile->image ().copy (tile->imageRect ());
7887 break ;
7988 case ColorRole:
89+ if (colorIndex == 0 )
90+ return QVariant ();
8091 return wangColorAt (index)->color ();
8192 }
8293
@@ -86,10 +97,13 @@ QVariant WangColorModel::data(const QModelIndex &index, int role) const
8697bool WangColorModel::setData (const QModelIndex &index, const QVariant &value, int role)
8798{
8899 if (role == Qt::EditRole) {
100+ const auto wangColor = wangColorAt (index);
101+ if (!wangColor)
102+ return false ;
103+
89104 const QString newName = value.toString ();
90- WangColor *wangColor = wangColorAt (index).data ();
91105 if (wangColor->name () != newName) {
92- auto command = new ChangeWangColorName (mTilesetDocument , wangColor, newName);
106+ auto command = new ChangeWangColorName (mTilesetDocument , wangColor. data () , newName);
93107 mTilesetDocument ->undoStack ()->push (command);
94108 }
95109
@@ -101,7 +115,11 @@ bool WangColorModel::setData(const QModelIndex &index, const QVariant &value, in
101115
102116Qt::ItemFlags WangColorModel::flags (const QModelIndex &index) const
103117{
104- return QAbstractItemModel::flags (index) | Qt::ItemIsEditable;
118+ Qt::ItemFlags flags = QAbstractItemModel::flags (index);
119+ if (index.row () == 0 )
120+ return flags;
121+
122+ return flags | Qt::ItemIsEditable;
105123}
106124
107125void WangColorModel::resetModel ()
@@ -110,20 +128,12 @@ void WangColorModel::resetModel()
110128 endResetModel ();
111129}
112130
113- int WangColorModel::colorAt (const QModelIndex &index) const
114- {
115- if (!index.isValid ())
116- return 0 ;
117-
118- return index.row () + 1 ;
119- }
120-
121131QSharedPointer<WangColor> WangColorModel::wangColorAt (const QModelIndex &index) const
122132{
123- if (!index.isValid ())
133+ if (!index.isValid () || index. row () == 0 )
124134 return QSharedPointer<WangColor>();
125135
126- return mWangSet ->colorAt (colorAt ( index));
136+ return mWangSet ->colorAt (index. row ( ));
127137}
128138
129139void WangColorModel::setName (WangColor *wangColor, const QString &name)
@@ -156,7 +166,7 @@ void WangColorModel::setProbability(WangColor *wangColor, qreal probability)
156166
157167void WangColorModel::emitDataChanged (WangColor *wangColor)
158168{
159- const QModelIndex i = colorIndex (wangColor->colorIndex ());
169+ const QModelIndex i = colorModelIndex (wangColor->colorIndex ());
160170 emit dataChanged (i, i);
161171}
162172
0 commit comments