diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-06-17 04:00:00 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-02 10:10:17 -0400 |
| commit | 6e2ae2444ca0bdea964cf0551edd338e6dafa2bc (patch) | |
| tree | 49682860b96f4d0cdee9239fb6526589804d7d7c | |
| parent | ab7f42dd97e5e6f6076877469a774213bfc3bf76 (diff) | |
split text file tab into its own class
| -rw-r--r-- | src/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/modinfodialog.cpp | 100 | ||||
| -rw-r--r-- | src/modinfodialog.h | 29 | ||||
| -rw-r--r-- | src/modinfodialogtextfiles.cpp | 91 | ||||
| -rw-r--r-- | src/modinfodialogtextfiles.h | 21 |
5 files changed, 180 insertions, 64 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d5aa8247..7e62f3e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -54,6 +54,7 @@ SET(organizer_SRCS modlist.cpp modidlineedit.cpp modinfodialog.cpp + modinfodialogtextfiles.cpp modinfo.cpp modinfobackup.cpp modinfoforeign.cpp @@ -154,6 +155,7 @@ SET(organizer_HDRS modlist.h modidlineedit.h modinfodialog.h + modinfodialogtextfiles.h modinfo.h modinfobackup.h modinfoforeign.h @@ -352,6 +354,7 @@ set(modinfo modinfo modinfobackup modinfodialog + modinfodialogtextfiles modinfoforeign modinfooverwrite modinforegular diff --git a/src/modinfodialog.cpp b/src/modinfodialog.cpp index 065058fc..7e72653f 100644 --- a/src/modinfodialog.cpp +++ b/src/modinfodialog.cpp @@ -38,6 +38,8 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include "previewdialog.h" #include "texteditor.h" +#include "modinfodialogtextfiles.h" + #include <QDir> #include <QDirIterator> #include <QPushButton> @@ -59,6 +61,18 @@ using namespace MOBase; using namespace MOShared; +std::vector<std::unique_ptr<ModInfoDialogTab>> ModInfoDialogTab::createTabs( + Ui::ModInfoDialog* ui) +{ + std::vector<std::unique_ptr<ModInfoDialogTab>> v; + + v.push_back(std::make_unique<TextFilesTab>(ui)); + + return v; +} + + + class ModFileListWidget : public QListWidgetItem { friend bool operator<(const ModFileListWidget &LHS, const ModFileListWidget &RHS); public: @@ -254,6 +268,9 @@ ModInfoDialog::ModInfoDialog(ModInfo::Ptr modInfo, const DirectoryEntry *directo m_OrganizerCore(organizerCore), m_PluginContainer(pluginContainer) { ui->setupUi(this); + + m_tabs = ModInfoDialogTab::createTabs(ui); + this->setWindowTitle(modInfo->name()); this->setWindowModality(Qt::WindowModal); @@ -305,12 +322,6 @@ ModInfoDialog::ModInfoDialog(ModInfo::Ptr modInfo, const DirectoryEntry *directo } } - ui->textFileView->setupToolbar(); - - ui->tabTextSplitter->setSizes({200, 1}); - ui->tabTextSplitter->setStretchFactor(0, 0); - ui->tabTextSplitter->setStretchFactor(1, 1); - // refresh everything but the conflict lists, which are done in exec() because // they depend on restoring the state to some widgets; this refresh has to be // done here because some of the checks below depend on the ui to decide which @@ -333,15 +344,17 @@ ModInfoDialog::ModInfoDialog(ModInfo::Ptr modInfo, const DirectoryEntry *directo } else if (unmanaged) { + ui->tabWidget->setTabEnabled(TAB_TEXTFILES, false); ui->tabWidget->setTabEnabled(TAB_INIFILES, false); ui->tabWidget->setTabEnabled(TAB_CATEGORIES, false); ui->tabWidget->setTabEnabled(TAB_NEXUS, false); ui->tabWidget->setTabEnabled(TAB_FILETREE, false); ui->tabWidget->setTabEnabled(TAB_NOTES, false); ui->tabWidget->setTabEnabled(TAB_ESPS, false); - ui->tabWidget->setTabEnabled(TAB_TEXTFILES, false); ui->tabWidget->setTabEnabled(TAB_IMAGES, false); } else { + ui->tabWidget->setTabEnabled(TAB_TEXTFILES, true); + initFiletree(modInfo); addCategories(CategoryFactory::instance(), modInfo->getCategories(), ui->categoriesTree->invisibleRootItem(), 0); refreshPrimaryCategoriesBox(); @@ -838,7 +851,10 @@ QTreeWidgetItem* ModInfoDialog::createAdvancedConflictItem( void ModInfoDialog::refreshFiles() { // clearing - ui->textFileList->clear(); + for (auto& tab : m_tabs) { + tab->clear(); + } + ui->iniTweaksList->clear(); ui->iniFileList->clear(); ui->inactiveESPList->clear(); @@ -857,9 +873,13 @@ void ModInfoDialog::refreshFiles() while (dirIterator.hasNext()) { QString fileName = dirIterator.next(); - if (fileName.endsWith(".txt", Qt::CaseInsensitive)) { - ui->textFileList->addItem(fileName.mid(m_RootPath.length() + 1)); - } else if ((fileName.endsWith(".ini", Qt::CaseInsensitive) || fileName.endsWith(".cfg", Qt::CaseInsensitive)) && + for (auto& tab : m_tabs) { + if (tab->feedFile(m_RootPath, fileName)) { + break; + } + } + + if ((fileName.endsWith(".ini", Qt::CaseInsensitive) || fileName.endsWith(".cfg", Qt::CaseInsensitive)) && !fileName.endsWith("meta.ini")) { QString namePart = fileName.mid(m_RootPath.length() + 1); if (namePart.startsWith("INI Tweaks", Qt::CaseInsensitive)) { @@ -903,7 +923,6 @@ void ModInfoDialog::refreshFiles() } } - ui->tabWidget->setTabEnabled(TAB_TEXTFILES, ui->textFileList->count() != 0); ui->tabWidget->setTabEnabled(TAB_IMAGES, ui->thumbnailArea->count() != 0); ui->tabWidget->setTabEnabled(TAB_ESPS, (ui->inactiveESPList->count() != 0) || (ui->activeESPList->count() != 0)); } @@ -943,9 +962,17 @@ void ModInfoDialog::saveCategories(QTreeWidgetItem *currentNode) void ModInfoDialog::on_closeButton_clicked() { - if (allowNavigateFromTXT() && allowNavigateFromINI()) { - this->close(); + for (auto& tab : m_tabs) { + if (!tab->canClose()) { + return; + } + } + + if (!allowNavigateFromINI()) { + return; } + + close(); } @@ -981,23 +1008,6 @@ void ModInfoDialog::thumbnailClicked(const QString &fileName) ui->imageLabel->setPixmap(QPixmap::fromImage(image)); } -bool ModInfoDialog::allowNavigateFromTXT() -{ - if (ui->textFileView->dirty()) { - const int res = QMessageBox::question( - this, tr("Save changes?"), - tr("Save changes to \"%1\"?").arg(ui->textFileView->filename()), - QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); - - if (res == QMessageBox::Cancel) { - return false; - } else if (res == QMessageBox::Yes) { - saveCurrentTextFile(); - } - } - return true; -} - bool ModInfoDialog::allowNavigateFromINI() { if (ui->saveButton->isEnabled()) { @@ -1012,29 +1022,6 @@ bool ModInfoDialog::allowNavigateFromINI() return true; } -void ModInfoDialog::on_textFileList_currentItemChanged( - QListWidgetItem *current, QListWidgetItem *previous) -{ - const QString fullPath = m_RootPath + "/" + current->text(); - - if (fullPath == ui->textFileView->filename()) { - // the new file is the same as the currently displayed file. May be the - // result of a cancellation - return; - } - - if (allowNavigateFromTXT()) { - openTextFile(fullPath); - } else { - ui->textFileList->setCurrentItem(previous, QItemSelectionModel::Current); - } -} - -void ModInfoDialog::openTextFile(const QString &fileName) -{ - ui->textFileView->load(fileName); -} - void ModInfoDialog::openIniFile(const QString &fileName) { QFile iniFile(fileName); @@ -1109,11 +1096,6 @@ void ModInfoDialog::on_saveButton_clicked() saveCurrentIniFile(); } -void ModInfoDialog::saveCurrentTextFile() -{ - ui->textFileView->save(); -} - void ModInfoDialog::saveCurrentIniFile() { diff --git a/src/modinfodialog.h b/src/modinfodialog.h index df450ac0..97ab9a38 100644 --- a/src/modinfodialog.h +++ b/src/modinfodialog.h @@ -53,9 +53,30 @@ class QTreeView; class CategoryFactory;
class TextEditor;
+class TextFilesTab;
+
+
+class ModInfoDialogTab
+{
+public:
+ static std::vector<std::unique_ptr<ModInfoDialogTab>> createTabs(
+ Ui::ModInfoDialog* ui);
+
+ ModInfoDialogTab() = default;
+ ModInfoDialogTab(const ModInfoDialogTab&) = delete;
+ ModInfoDialogTab& operator=(const ModInfoDialogTab&) = delete;
+ ModInfoDialogTab(ModInfoDialogTab&&) = default;
+ ModInfoDialogTab& operator=(ModInfoDialogTab&&) = default;
+ virtual ~ModInfoDialogTab() = default;
+
+ virtual void clear() = 0;
+ virtual bool feedFile(const QString& rootPath, const QString& filename) = 0;
+ virtual bool canClose() = 0;
+};
+
/**
- * this is a larger dialog used to visualise information abount the mod.
+ * this is a larger dialog used to visualise information about the mod.
* @todo this would probably a good place for a plugin-system
**/
class ModInfoDialog : public MOBase::TutorableDialog
@@ -147,11 +168,8 @@ private: void deleteFile(const QModelIndex &index);
void saveIniTweaks();
void saveCategories(QTreeWidgetItem *currentNode);
- void saveCurrentTextFile();
void saveCurrentIniFile();
- void openTextFile(const QString &fileName);
void openIniFile(const QString &fileName);
- bool allowNavigateFromTXT();
bool allowNavigateFromINI();
FileRenamer::RenameResults hideFile(FileRenamer& renamer, const QString &oldName);
FileRenamer::RenameResults unhideFile(FileRenamer& renamer, const QString &oldName);
@@ -189,7 +207,6 @@ private slots: void on_tabWidget_currentChanged(int index);
void on_primaryCategoryBox_currentIndexChanged(int index);
void on_categoriesTree_itemChanged(QTreeWidgetItem *item, int column);
- void on_textFileList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void on_iniFileList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void on_iniTweaksList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous);
void on_overwriteTree_itemDoubleClicked(QTreeWidgetItem *item, int column);
@@ -234,6 +251,8 @@ private: ModInfo::Ptr m_ModInfo;
+ std::vector<std::unique_ptr<ModInfoDialogTab>> m_tabs;
+
QSignalMapper m_ThumbnailMapper;
QString m_RootPath;
diff --git a/src/modinfodialogtextfiles.cpp b/src/modinfodialogtextfiles.cpp new file mode 100644 index 00000000..98da8c4a --- /dev/null +++ b/src/modinfodialogtextfiles.cpp @@ -0,0 +1,91 @@ +#include "modinfodialogtextfiles.h" +#include "ui_modinfodialog.h" +#include <QMessageBox> + +class TextFileItem : public QListWidgetItem +{ +public: + TextFileItem(const QString& rootPath, QString fullPath) + : m_fullPath(std::move(fullPath)) + { + setText(m_fullPath.mid(rootPath.length() + 1)); + } + + const QString& fullPath() const + { + return m_fullPath; + } + +private: + QString m_fullPath; +}; + + +TextFilesTab::TextFilesTab(Ui::ModInfoDialog* ui) + : ui(ui) +{ + ui->textFileView->setupToolbar(); + + ui->tabTextSplitter->setSizes({200, 1}); + ui->tabTextSplitter->setStretchFactor(0, 0); + ui->tabTextSplitter->setStretchFactor(1, 1); + + QObject::connect( + ui->textFileList, &QListWidget::currentItemChanged, + [&](auto* current, auto* previous){ onSelection(current, previous); }); +} + +void TextFilesTab::clear() +{ + ui->textFileList->clear(); +} + +bool TextFilesTab::feedFile(const QString& rootPath, const QString& fullPath) +{ + if (fullPath.endsWith(".txt", Qt::CaseInsensitive)) { + ui->textFileList->addItem(new TextFileItem(rootPath, fullPath)); + return true; + } + + return false; +} + +bool TextFilesTab::canClose() +{ + if (!ui->textFileView->dirty()) { + return true; + } + + const int res = QMessageBox::question( + ui->tabText, + QObject::tr("Save changes?"), + QObject::tr("Save changes to \"%1\"?").arg(ui->textFileView->filename()), + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + + if (res == QMessageBox::Cancel) { + return false; + } + + if (res == QMessageBox::Yes) { + ui->textFileView->save(); + } + + return true; +} + +void TextFilesTab::onSelection( + QListWidgetItem* current, QListWidgetItem* previous) +{ + auto* item = dynamic_cast<TextFileItem*>(current); + if (!item) { + qCritical("TextFilesTab: item is not a TextFileItem"); + return; + } + + if (!canClose()) { + ui->textFileList->setCurrentItem(previous, QItemSelectionModel::Current); + return; + } + + ui->textFileView->load(item->fullPath()); +} diff --git a/src/modinfodialogtextfiles.h b/src/modinfodialogtextfiles.h new file mode 100644 index 00000000..ab9e972c --- /dev/null +++ b/src/modinfodialogtextfiles.h @@ -0,0 +1,21 @@ +#ifndef MODINFODIALOGTEXTFILES_H +#define MODINFODIALOGTEXTFILES_H + +#include "modinfodialog.h" + +class TextFilesTab : public ModInfoDialogTab +{ +public: + TextFilesTab(Ui::ModInfoDialog* ui); + + void clear() override; + bool feedFile(const QString& rootPath, const QString& fullPath) override; + bool canClose() override; + +private: + Ui::ModInfoDialog* ui; + + void onSelection(QListWidgetItem* current, QListWidgetItem* previous); +}; + +#endif // MODINFODIALOGTEXTFILES_H |
