From 9365973fcd739822c180c9786ff48dd8892c6cfb Mon Sep 17 00:00:00 2001 From: dfighter1985 Date: Thu, 24 Jul 2014 02:28:19 +0200 Subject: [PATCH] Implemented tileset move up and move down. --- .../tile_editor/tile_editor_main_window.cpp | 62 +++++++++++++------ .../tile_editor/tile_editor_main_window.h | 2 + .../tile_editor/tile_editor_main_window.ui | 4 +- .../src/plugins/tile_editor/tile_item.cpp | 7 +++ .../src/plugins/tile_editor/tile_item.h | 2 + .../src/plugins/tile_editor/tile_model.cpp | 5 ++ .../src/plugins/tile_editor/tile_model.h | 2 + 7 files changed, 63 insertions(+), 21 deletions(-) diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp index 8ab499bf8..88090de47 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.cpp @@ -87,9 +87,13 @@ TileEditorMainWindow::TileEditorMainWindow(QWidget *parent) // Set up the tile set list view. m_ui->tileSetLV->setModel(m_model); //m_ui->tileSetLV->setRootIndex(m_model->index(0,0)); + connect(m_ui->tileSetAddTB, SIGNAL(clicked()), this, SLOT(onTileSetAdd())); connect(m_ui->tileSetDeleteTB, SIGNAL(clicked()), this, SLOT(onTileSetDelete())); connect(m_ui->tileSetEditTB, SIGNAL(clicked()), this, SLOT(onTileSetEdit())); + connect(m_ui->tileSetUpTB, SIGNAL(clicked()), this, SLOT(onTileSetUp())); + connect(m_ui->tileSetDownTB, SIGNAL(clicked()), this, SLOT(onTileSetDown())); + connect(m_ui->tileSetLV->selectionModel(), SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)), this, SLOT(changeActiveTileSet(const QModelIndex &, const QModelIndex &))); @@ -236,23 +240,6 @@ void TileEditorMainWindow::onTileSetAdd() return; } - //if(index.isValid()) - //{ - // if(!model->insertRow(index.row()+1, index.parent())) - // return; - - // //updateActions() - - // for(int column=0; columncolumnCount(index.parent()); column++) - // { - // QModelIndex child = model->index(index.row()+1, column, index.parent()); - // model->setData(child, QVariant(text), Qt::EditRole); - // } - //} - //else - //{ - - // Create and append the new tile set to the model. TileSetNode *tileSet = model->createTileSetNode(text); @@ -271,8 +258,6 @@ void TileEditorMainWindow::onTileSetDelete() TileModel *model = static_cast(m_ui->tileSetLV->model()); bool ok = model->removeRow( idx.row() ); - - //m_ui->tileSetLV->reset(); } void TileEditorMainWindow::onTileSetEdit() @@ -309,6 +294,45 @@ void TileEditorMainWindow::onTileSetEdit() m_ui->tileSetLV->reset(); } +void TileEditorMainWindow::onTileSetUp() +{ + QModelIndex idx = m_ui->tileSetLV->currentIndex(); + if( !idx.isValid() ) + return; + + if( idx.row() == 0 ) + return; + + TileModel *model = static_cast(m_ui->tileSetLV->model()); + if( model->rowCount() < 2 ) + return; + + int r = idx.row(); + model->swapRows( r, r - 1 ); + + m_ui->tileSetLV->reset(); + m_ui->tileSetLV->setCurrentIndex( model->index( r - 1, 0 ) ); +} + +void TileEditorMainWindow::onTileSetDown() +{ + QModelIndex idx = m_ui->tileSetLV->currentIndex(); + if( !idx.isValid() ) + return; + + TileModel *model = static_cast(m_ui->tileSetLV->model()); + if( model->rowCount() < idx.row() ) + return; + if( model->rowCount() < 2 ) + return; + + int r = idx.row(); + model->swapRows( r, r + 1 ); + + m_ui->tileSetLV->reset(); + m_ui->tileSetLV->setCurrentIndex( model->index( r + 1, 0 ) ); +} + void TileEditorMainWindow::onActionAddTile(int tabId) { QFileDialog::Options options; diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h index 8719f799a..c830666a0 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.h +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.h @@ -50,6 +50,8 @@ public Q_SLOTS: void onTileSetAdd(); void onTileSetDelete(); void onTileSetEdit(); + void onTileSetUp(); + void onTileSetDown(); void changeActiveTileSet(const QModelIndex &newIndex, const QModelIndex &oldIndex); void onZoomFactor(int level); diff --git a/code/studio/src/plugins/tile_editor/tile_editor_main_window.ui b/code/studio/src/plugins/tile_editor/tile_editor_main_window.ui index a88c11d0e..f90ac69fb 100644 --- a/code/studio/src/plugins/tile_editor/tile_editor_main_window.ui +++ b/code/studio/src/plugins/tile_editor/tile_editor_main_window.ui @@ -442,7 +442,7 @@ - + ... @@ -453,7 +453,7 @@ - + ... diff --git a/code/studio/src/plugins/tile_editor/tile_item.cpp b/code/studio/src/plugins/tile_editor/tile_item.cpp index ef80cc302..230776fbd 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.cpp +++ b/code/studio/src/plugins/tile_editor/tile_item.cpp @@ -155,6 +155,13 @@ void Node::appendRow(Node *item) m_childItems.append(item); } +void Node::swapRows( int a, int b ) +{ + Node *temp = m_childItems[ a ]; + m_childItems[ a ] = m_childItems[ b ]; + m_childItems[ b ] = temp; +} + /////////////////////////////////////////////////// TileSetNode::TileSetNode(QString tileSetName, Node *parent) : m_tileSetName(tileSetName) diff --git a/code/studio/src/plugins/tile_editor/tile_item.h b/code/studio/src/plugins/tile_editor/tile_item.h index 69fc89d71..56adfa926 100644 --- a/code/studio/src/plugins/tile_editor/tile_item.h +++ b/code/studio/src/plugins/tile_editor/tile_item.h @@ -53,6 +53,8 @@ public: void appendRow(const QList &items); void appendRow(Node *item); + void swapRows( int a, int b ); + protected: QList m_childItems; QVector m_itemData; diff --git a/code/studio/src/plugins/tile_editor/tile_model.cpp b/code/studio/src/plugins/tile_editor/tile_model.cpp index a5527fb56..4b6d2113d 100644 --- a/code/studio/src/plugins/tile_editor/tile_model.cpp +++ b/code/studio/src/plugins/tile_editor/tile_model.cpp @@ -155,6 +155,11 @@ bool TileModel::removeRows( int row, int count, const QModelIndex &parent ) return ok; } +void TileModel::swapRows( int a, int b ) +{ + rootItem->swapRows( a, b ); +} + TileSetNode *TileModel::createTileSetNode(QString tileSetName) { // Create the new tile set. diff --git a/code/studio/src/plugins/tile_editor/tile_model.h b/code/studio/src/plugins/tile_editor/tile_model.h index cdf85acc3..239c423cb 100644 --- a/code/studio/src/plugins/tile_editor/tile_model.h +++ b/code/studio/src/plugins/tile_editor/tile_model.h @@ -82,6 +82,8 @@ public: bool removeRows( int row, int count, const QModelIndex &parent = QModelIndex() ); + void swapRows( int a, int b ); + TileSetNode *createTileSetNode(QString tileSetName); static const char *getTileTypeName(TNodeTileType type);