From 362976328ecb796d670b32af924f76e25922cb78 Mon Sep 17 00:00:00 2001 From: Treeki Date: Fri, 3 Aug 2012 22:08:22 +0200 Subject: added Rename and Remove Item functions --- lsmainwindow.cpp | 186 ++++++++++++++++++++++++++++++++++------------------- lsmainwindow.h | 6 ++ lspackagemodel.cpp | 13 ++++ lspackagemodel.h | 5 +- 4 files changed, 142 insertions(+), 68 deletions(-) diff --git a/lsmainwindow.cpp b/lsmainwindow.cpp index 735d7a3..66c51b3 100644 --- a/lsmainwindow.cpp +++ b/lsmainwindow.cpp @@ -20,24 +20,25 @@ #include #include #include +#include #include "lyt/archivepackage.h" LSMainWindow::LSMainWindow(QWidget *parent) : QMainWindow(parent) { - m_package = 0; - m_dirty = false; - m_isSaved = false; + m_package = 0; + m_dirty = false; + m_isSaved = false; createActions(); m_view = new QTreeView(this); setCentralWidget(m_view); - newArchive(); + newArchive(); } LSMainWindow::~LSMainWindow() { - if (m_package) - delete m_package; + if (m_package) + delete m_package; } @@ -71,6 +72,9 @@ void LSMainWindow::createActions() { connect(m_addTextureAction, SIGNAL(triggered()), m_addActionMapper, SLOT(map())); connect(m_addActionMapper, SIGNAL(mapped(int)), SLOT(handleAddSomething(int))); + connect(m_renameAction, SIGNAL(triggered()), SLOT(handleRenameItem())); + connect(m_removeAction, SIGNAL(triggered()), SLOT(handleRemoveItem())); + QMenuBar *bar = menuBar(); QMenu *m; @@ -130,94 +134,142 @@ void LSMainWindow::handleAddSomething(int whatToAdd) { } +void LSMainWindow::handleRemoveItem() { + QString what = selectedItem(); + LYTPackageBase::ItemType whatType = selectedItemType(); + + if (!what.isEmpty()) { + QString confirmText = QString("Are you sure you want to delete '%1'? You'll lose it forever...!").arg(what); + + int confirm = QMessageBox::question(this, + "Confirm Removal", + confirmText, + QMessageBox::Yes, + QMessageBox::No); + + if (confirm == QMessageBox::Yes) { + m_package->remove(whatType, what); + } + } +} + + +void LSMainWindow::handleRenameItem() { + QString what = selectedItem(); + LYTPackageBase::ItemType whatType = selectedItemType(); + + if (!what.isEmpty()) { + QString newName = QInputDialog::getText(this, + "Rename Item", + QString("Enter a new name:"), + QLineEdit::NoEcho, + what); + + if (!newName.isEmpty()) { + m_package->rename(whatType, what, newName); + } + } +} + + +QString LSMainWindow::selectedItem() const { + QModelIndex idx = m_view->currentIndex(); + return m_model->itemNameForIndex(idx); +} + +LYTPackageBase::ItemType LSMainWindow::selectedItemType() const { + QModelIndex idx = m_view->currentIndex(); + return m_model->itemTypeForIndex(idx); +} + bool LSMainWindow::ensureSaved() { - // TODO - return false; + // TODO + return false; } void LSMainWindow::updateTitleBar() { - QString title; - if (m_isSaved) - title = m_package->description(); - else - title = "[Unsaved]"; - - if (m_dirty) - title.append('*'); - title.append(" - LayoutStudio"); - - setWindowTitle(title); - setWindowModified(m_dirty); + QString title; + if (m_isSaved) + title = m_package->description(); + else + title = "[Unsaved]"; + + if (m_dirty) + title.append('*'); + title.append(" - LayoutStudio"); + + setWindowTitle(title); + setWindowModified(m_dirty); } void LSMainWindow::newArchive() { - if (ensureSaved()) - return; + if (ensureSaved()) + return; - LYTArchivePackage *pkg = new LYTArchivePackage; - setCurrentPackage(pkg); + LYTArchivePackage *pkg = new LYTArchivePackage; + setCurrentPackage(pkg); - m_dirty = false; - m_isSaved = false; - updateTitleBar(); + m_dirty = false; + m_isSaved = false; + updateTitleBar(); } void LSMainWindow::openArchive() { - if (ensureSaved()) - return; + if (ensureSaved()) + return; - QString path = QFileDialog::getOpenFileName(this, - "Open a Layout Archive", - QString(), - "Wii Archives (*.arc)" - ); + QString path = QFileDialog::getOpenFileName(this, + "Open a Layout Archive", + QString(), + "Wii Archives (*.arc)" + ); - if (path.isEmpty()) - return; + if (path.isEmpty()) + return; - LYTArchivePackage *pkg = new LYTArchivePackage(path); - setCurrentPackage(pkg); + LYTArchivePackage *pkg = new LYTArchivePackage(path); + setCurrentPackage(pkg); - m_dirty = false; - m_isSaved = false; - updateTitleBar(); + m_dirty = false; + m_isSaved = false; + updateTitleBar(); } void LSMainWindow::save() { - // TODO: check that m_package is an arc - if (m_isSaved) { - m_dirty = false; - m_package->savePackage(); - updateTitleBar(); - } else { - saveArchiveAs(); - } + // TODO: check that m_package is an arc + if (m_isSaved) { + m_dirty = false; + m_package->savePackage(); + updateTitleBar(); + } else { + saveArchiveAs(); + } } void LSMainWindow::saveArchiveAs() { - // TODO: check that m_package is an arc - QString newPath = QFileDialog::getSaveFileName(this, - "Save Layout Archive", - QString(), - "Wii Archives (*.arc)" - ); - - if (newPath.isEmpty()) - return; - - LYTArchivePackage *pkg = (LYTArchivePackage*)m_package; - pkg->setFilename(newPath); - pkg->savePackage(); - - m_dirty = false; - m_isSaved = true; - updateTitleBar(); + // TODO: check that m_package is an arc + QString newPath = QFileDialog::getSaveFileName(this, + "Save Layout Archive", + QString(), + "Wii Archives (*.arc)" + ); + + if (newPath.isEmpty()) + return; + + LYTArchivePackage *pkg = (LYTArchivePackage*)m_package; + pkg->setFilename(newPath); + pkg->savePackage(); + + m_dirty = false; + m_isSaved = true; + updateTitleBar(); } void LSMainWindow::setCurrentPackage(LYTPackageBase *pkg) { - m_package = pkg; + m_package = pkg; QItemSelectionModel *oldSel = m_view->selectionModel(); QAbstractItemModel *model = m_view->model(); diff --git a/lsmainwindow.h b/lsmainwindow.h index f989e7a..745b4f2 100644 --- a/lsmainwindow.h +++ b/lsmainwindow.h @@ -44,6 +44,8 @@ public slots: private slots: void handleAddSomething(int whatToAdd); + void handleRenameItem(); + void handleRemoveItem(); private: LYTPackageBase *m_package; @@ -64,6 +66,10 @@ private: bool m_dirty; bool m_isSaved; + +protected: + QString selectedItem() const; + LYTPackageBase::ItemType selectedItemType() const; }; #endif // LSMAINWINDOW_H diff --git a/lspackagemodel.cpp b/lspackagemodel.cpp index d39bb89..9580e91 100644 --- a/lspackagemodel.cpp +++ b/lspackagemodel.cpp @@ -148,3 +148,16 @@ void LSPackageModel::handleFileWasRenamed(LYTPackageBase::ItemType type, QString if (toIdx != fromIdx) endMoveRows(); } + + +QString LSPackageModel::itemNameForIndex(const QModelIndex &index) const { + if (index.internalId() > 0) + return m_caches[index.internalId() - 1].at(index.row()); + return QString(); +} + +LYTPackageBase::ItemType LSPackageModel::itemTypeForIndex(const QModelIndex &index) const { + if (index.internalId() > 0) + return ContentKinds[index.internalId() - 1].type; + return (LYTPackageBase::ItemType)-1; +} diff --git a/lspackagemodel.h b/lspackagemodel.h index 18911de..58ad62d 100644 --- a/lspackagemodel.h +++ b/lspackagemodel.h @@ -17,7 +17,10 @@ public: int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; - + + QString itemNameForIndex(const QModelIndex &index) const; + LYTPackageBase::ItemType itemTypeForIndex(const QModelIndex &index) const; + signals: private slots: -- cgit v1.2.3