From 93910a2130dcc7b22212d75a635de14328ea943d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 6 Sep 2019 22:18:18 +0300 Subject: [PATCH] Improve for recent files list. Show duplicate file names with unique path section. --HG-- branch : develop --- ChangeLog.txt | 1 + src/app/tape/tmainwindow.cpp | 39 ++---- src/app/tape/tmainwindow.h | 6 +- src/app/valentina/mainwindow.cpp | 48 ++----- src/app/valentina/mainwindow.h | 4 - src/app/valentina/mainwindowsnogui.cpp | 6 + src/app/valentina/mainwindowsnogui.h | 1 + src/libs/vwidgets/vabstractmainwindow.cpp | 150 +++++++++++++++++++++- src/libs/vwidgets/vabstractmainwindow.h | 6 + 9 files changed, 187 insertions(+), 74 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index d5ca449f4..1cfda2dac 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -30,6 +30,7 @@ - All intersections are now treaded as a loop. - [#558] New export: Export pattern as step-by-step text. - Added options to control passmark angle type and passmark mark type. +- Improve for recent files list. Show duplicate file names with unique path section. # Version 0.6.2 (unreleased) - [#903] Bug in tool Cut Spline path. diff --git a/src/app/tape/tmainwindow.cpp b/src/app/tape/tmainwindow.cpp index 10ac9c6f1..a83540369 100644 --- a/src/app/tape/tmainwindow.cpp +++ b/src/app/tape/tmainwindow.cpp @@ -104,8 +104,6 @@ TMainWindow::TMainWindow(QWidget *parent) labelPatternUnit(nullptr), isInitialized(false), mIsReadOnly(false), - recentFileActs(QVector(MaxRecentFiles)), - separatorAct(nullptr), hackedWidgets() { ui->setupUi(this); @@ -127,7 +125,7 @@ TMainWindow::TMainWindow(QWidget *parent) ui->mainToolBar->setContextMenuPolicy(Qt::PreventContextMenu); ui->toolBarGradation->setContextMenuPolicy(Qt::PreventContextMenu); - recentFileActs.fill(nullptr); + m_recentFileActs.fill(nullptr); SetupMenu(); UpdateWindowTitle(); @@ -689,6 +687,12 @@ void TMainWindow::ExportToCSVData(const QString &fileName, bool withHeader, int csv.toCSV(fileName, error, withHeader, separator, QTextCodec::codecForMib(mib)); } +//--------------------------------------------------------------------------------------------------------------------- +QStringList TMainWindow::RecentFileList() const +{ + return qApp->TapeSettings()->GetRecentFileList(); +} + //--------------------------------------------------------------------------------------------------------------------- bool TMainWindow::FileSave() { @@ -1940,7 +1944,7 @@ void TMainWindow::SetupMenu() }); connect(ui->actionPreferences, &QAction::triggered, this, &TMainWindow::Preferences); - for (auto & recentFileAct : recentFileActs) + for (auto & recentFileAct : m_recentFileActs) { auto *action = new QAction(this); recentFileAct = action; @@ -1959,10 +1963,10 @@ void TMainWindow::SetupMenu() recentFileAct->setVisible(false); } - separatorAct = new QAction(this); - separatorAct->setSeparator(true); - separatorAct->setVisible(false); - ui->menuFile->insertAction(ui->actionPreferences, separatorAct ); + m_separatorAct = new QAction(this); + m_separatorAct->setSeparator(true); + m_separatorAct->setVisible(false); + ui->menuFile->insertAction(ui->actionPreferences, m_separatorAct ); connect(ui->actionQuit, &QAction::triggered, this, &TMainWindow::close); @@ -2916,25 +2920,6 @@ bool TMainWindow::LoadFromExistingFile(const QString &path) return true; } -//--------------------------------------------------------------------------------------------------------------------- -void TMainWindow::UpdateRecentFileActions() -{ - qCDebug(tMainWindow, "Updating recent file actions."); - const QStringList files = qApp->TapeSettings()->GetRecentFileList(); - const int numRecentFiles = qMin(files.size(), static_cast(MaxRecentFiles)); - qCDebug(tMainWindow, "Updating recent file actions = %i ",numRecentFiles); - - for (int i = 0; i < numRecentFiles; ++i) - { - const QString text = QStringLiteral("&%1. %2").arg(i + 1).arg(StrippedName(files.at(i))); - qCDebug(tMainWindow, "file %i = %s", numRecentFiles, qUtf8Printable(text)); - recentFileActs.at(i)->setText(text); - recentFileActs.at(i)->setData(files.at(i)); - recentFileActs.at(i)->setVisible(true); - } - - separatorAct->setVisible(numRecentFiles>0); -} //--------------------------------------------------------------------------------------------------------------------- void TMainWindow::CreateWindowMenu(QMenu *menu) diff --git a/src/app/tape/tmainwindow.h b/src/app/tape/tmainwindow.h index 35bef80c0..3a83741aa 100644 --- a/src/app/tape/tmainwindow.h +++ b/src/app/tape/tmainwindow.h @@ -72,6 +72,7 @@ protected: virtual bool eventFilter(QObject *object, QEvent *event) override; virtual void ExportToCSVData(const QString &fileName, bool withHeader, int mib, const QChar &separator) final; + virtual QStringList RecentFileList() const override; private slots: void FileNew(); @@ -148,9 +149,7 @@ private: QLabel *labelPatternUnit; bool isInitialized; bool mIsReadOnly; - enum { MaxRecentFiles = 5 }; - QVector recentFileActs; - QAction *separatorAct; + QVector hackedWidgets; void SetupMenu(); @@ -164,7 +163,6 @@ private: void ShowNewMData(bool fresh); void ShowUnits(); void ShowHeaderUnits(QTableWidget *table, int column, const QString &unit); - void UpdateRecentFileActions(); void MeasurementsWereSaved(bool saved); void SetCurrentFile(const QString &fileName); diff --git a/src/app/valentina/mainwindow.cpp b/src/app/valentina/mainwindow.cpp index c327922de..fa039ec9f 100644 --- a/src/app/valentina/mainwindow.cpp +++ b/src/app/valentina/mainwindow.cpp @@ -176,8 +176,7 @@ MainWindow::MainWindow(QWidget *parent) currentDrawIndex(0), currentToolBoxIndex(0), isDockToolOptionsVisible(true), isDockGroupsVisible(true), - drawMode(true), recentFileActs(), - separatorAct(nullptr), + drawMode(true), leftGoToStage(nullptr), rightGoToStage(nullptr), autoSaveTimer(nullptr), guiEnabled(true), gradationHeights(nullptr), gradationSizes(nullptr), @@ -193,11 +192,6 @@ MainWindow::MainWindow(QWidget *parent) m_progressBar(new QProgressBar(this)), m_statusLabel(new QLabel(this)) { - for (int i = 0; i < MaxRecentFiles; ++i) - { - recentFileActs[i] = nullptr; - } - CreateActions(); InitScenes(); @@ -4011,38 +4005,16 @@ bool MainWindow::MaybeSave() return true; } -//--------------------------------------------------------------------------------------------------------------------- -void MainWindow::UpdateRecentFileActions() -{ - qCDebug(vMainWindow, "Updating recent file actions."); - const QStringList files = qApp->ValentinaSettings()->GetRecentFileList(); - const int numRecentFiles = qMin(files.size(), static_cast(MaxRecentFiles)); - - for (int i = 0; i < numRecentFiles; ++i) - { - QString text = QString("&%1. %2").arg(i + 1).arg(StrippedName(files.at(i))); - recentFileActs[i]->setText(text); - recentFileActs[i]->setData(files.at(i)); - recentFileActs[i]->setVisible(true); - } - for (int j = numRecentFiles; j < MaxRecentFiles; ++j) - { - recentFileActs[j]->setVisible(false); - } - - separatorAct->setVisible(numRecentFiles > 0); -} - //--------------------------------------------------------------------------------------------------------------------- void MainWindow::CreateMenus() { for (int i = 0; i < MaxRecentFiles; ++i) { - ui->menuFile->insertAction(ui->actionPreferences, recentFileActs[i]); + ui->menuFile->insertAction(ui->actionPreferences, m_recentFileActs.at(i)); } - separatorAct = new QAction(this); - separatorAct->setSeparator(true); - ui->menuFile->insertAction(ui->actionPreferences, separatorAct); + m_separatorAct = new QAction(this); + m_separatorAct->setSeparator(true); + ui->menuFile->insertAction(ui->actionPreferences, m_separatorAct); UpdateRecentFileActions(); //Add Undo/Redo actions. @@ -4060,9 +4032,9 @@ void MainWindow::CreateMenus() ui->menuPatternPiece->insertAction(ui->actionLast_tool, redoAction); ui->toolBarTools->addAction(redoAction); - separatorAct = new QAction(this); - separatorAct->setSeparator(true); - ui->menuPatternPiece->insertAction(ui->actionPattern_properties, separatorAct); + m_separatorAct = new QAction(this); + m_separatorAct->setSeparator(true); + ui->menuPatternPiece->insertAction(ui->actionPattern_properties, m_separatorAct); AddDocks(); } @@ -4559,8 +4531,8 @@ void MainWindow::CreateActions() { QAction *action = new QAction(this); action->setVisible(false); - recentFileActs[i] = action; - connect(recentFileActs[i], &QAction::triggered, this, [this]() + m_recentFileActs[i] = action; + connect(m_recentFileActs[i], &QAction::triggered, this, [this]() { if (QAction *action = qobject_cast(sender())) { diff --git a/src/app/valentina/mainwindow.h b/src/app/valentina/mainwindow.h index 407e81f8e..c2f60dbd0 100644 --- a/src/app/valentina/mainwindow.h +++ b/src/app/valentina/mainwindow.h @@ -255,9 +255,6 @@ private: /** @brief drawMode true if we current draw scene. */ bool drawMode; - enum { MaxRecentFiles = 5 }; - QAction *recentFileActs[MaxRecentFiles]; - QAction *separatorAct; QLabel *leftGoToStage; QLabel *rightGoToStage; QTimer *autoSaveTimer; @@ -326,7 +323,6 @@ private: void WriteSettings(); bool MaybeSave(); - void UpdateRecentFileActions(); void CreateMenus(); void CreateActions(); void InitAutoSave(); diff --git a/src/app/valentina/mainwindowsnogui.cpp b/src/app/valentina/mainwindowsnogui.cpp index 9db91dbf5..ab6182826 100644 --- a/src/app/valentina/mainwindowsnogui.cpp +++ b/src/app/valentina/mainwindowsnogui.cpp @@ -1038,6 +1038,12 @@ void MainWindowsNoGUI::InitTempLayoutScene() tempSceneLayout->setBackgroundBrush( QBrush(QColor(Qt::gray), Qt::SolidPattern) ); } +//--------------------------------------------------------------------------------------------------------------------- +QStringList MainWindowsNoGUI::RecentFileList() const +{ + return qApp->ValentinaSettings()->GetRecentFileList(); +} + //--------------------------------------------------------------------------------------------------------------------- QIcon MainWindowsNoGUI::ScenePreview(int i, QSize iconSize, PreviewQuatilty quality) const { diff --git a/src/app/valentina/mainwindowsnogui.h b/src/app/valentina/mainwindowsnogui.h index c80ae6ba8..599fe384c 100644 --- a/src/app/valentina/mainwindowsnogui.h +++ b/src/app/valentina/mainwindowsnogui.h @@ -135,6 +135,7 @@ protected: void InitTempLayoutScene(); virtual void CleanLayout()=0; virtual void PrepareSceneList(PreviewQuatilty quality)=0; + virtual QStringList RecentFileList() const override; QIcon ScenePreview(int i, QSize iconSize, PreviewQuatilty quality) const; bool GenerateLayout(VLayoutGenerator& lGenerator); int ContinueIfLayoutStale(); diff --git a/src/libs/vwidgets/vabstractmainwindow.cpp b/src/libs/vwidgets/vabstractmainwindow.cpp index 9ec962dca..b335cb125 100644 --- a/src/libs/vwidgets/vabstractmainwindow.cpp +++ b/src/libs/vwidgets/vabstractmainwindow.cpp @@ -40,11 +40,137 @@ #include #endif +namespace +{ +//--------------------------------------------------------------------------------------------------------------------- +QStringList SplitFilePaths(const QString &path) +{ + QStringList result; + QString subPath = QDir::cleanPath(path); + QString lastFileName; + + do + { + QFileInfo fileInfo(subPath); + lastFileName = fileInfo.fileName(); + if (not lastFileName.isEmpty()) + { + result.prepend(lastFileName); + subPath = fileInfo.path(); + } + } + while(not lastFileName.isEmpty()); + + return result; +} + +//--------------------------------------------------------------------------------------------------------------------- +QStringList ReverseList(const QStringList &list) +{ + if (list.isEmpty()) + { + return list; + } + QVector reversedList(list.size()); + qint32 j = 0; + for (qint32 i = list.size() - 1; i >= 0; --i) + { + reversedList.replace(j, list.at(i)); + ++j; + } + return reversedList.toList(); +} + +//--------------------------------------------------------------------------------------------------------------------- +QStringList SelectNumber(QStringList path, int number) +{ + path = ReverseList(path); + QStringList subPath = path.mid(0, number); + return ReverseList(subPath); +} + +//--------------------------------------------------------------------------------------------------------------------- +QStringList RecentFiles(const QStringList &paths) +{ + QVector table; + table.reserve(paths.size()); + + for(auto &path : paths) + { + table.append(SplitFilePaths(path)); + } + + auto CreateOptimized = [table](int tableRow) + { + QStringList optimized; + bool isUnique = true; + + QStringList path = table.at(tableRow); + for (int count = 1; count <= path.size(); ++count) + { + isUnique = true; + optimized = SelectNumber(path, count); + + for (int row = 0; row < table.size(); ++row) + { + if (row == tableRow) + { + continue; + } + + QStringList testSubPath = SelectNumber(table.at(row), count); + if (optimized.join(QDir::separator()) == testSubPath.join(QDir::separator())) + { + isUnique = false; + break; + } + } + + if (isUnique) + { + break; + } + } + + if (optimized.size() >= 3) + { + optimized = QStringList({optimized.first(), QStringLiteral("…"), optimized.last()}); + } + + return optimized; + }; + + QVector optimizedPaths; + optimizedPaths.reserve(paths.size()); + + for (int row = 0; row < table.size(); ++row) + { + optimizedPaths.append(CreateOptimized(row)); + } + + QStringList recentFiles; + recentFiles.reserve(paths.size()); + + for(auto &path : optimizedPaths) + { + recentFiles.append(path.join(QDir::separator())); + } + + return recentFiles; +} +} + +//--------------------------------------------------------------------------------------------------------------------- VAbstractMainWindow::VAbstractMainWindow(QWidget *parent) : QMainWindow(parent), m_curFileFormatVersion(0x0), m_curFileFormatVersionStr(QLatin1String("0.0.0")) -{} +{ + for (int i = 0; i < MaxRecentFiles; ++i) + { + m_recentFileActs[i] = nullptr; + } +} //--------------------------------------------------------------------------------------------------------------------- bool VAbstractMainWindow::ContinueFormatRewrite(const QString ¤tFormatVersion, @@ -126,6 +252,28 @@ QString VAbstractMainWindow::CSVFilePath() return fileName; } +//--------------------------------------------------------------------------------------------------------------------- +void VAbstractMainWindow::UpdateRecentFileActions() +{ + const QStringList files = RecentFileList(); + const QStringList recentFiles = RecentFiles(files); + const int numRecentFiles = qMin(files.size(), static_cast(MaxRecentFiles)); + + for (int i = 0; i < numRecentFiles; ++i) + { + QString recent = recentFiles.at(i); + if (not recent.isEmpty()) + { + const QString text = QStringLiteral("&%1. %2").arg(i + 1).arg(recentFiles.at(i)); + m_recentFileActs.at(i)->setText(text); + m_recentFileActs.at(i)->setData(files.at(i)); + m_recentFileActs.at(i)->setVisible(true); + } + } + + m_separatorAct->setVisible(numRecentFiles>0); +} + //--------------------------------------------------------------------------------------------------------------------- void VAbstractMainWindow::WindowsLocale() { diff --git a/src/libs/vwidgets/vabstractmainwindow.h b/src/libs/vwidgets/vabstractmainwindow.h index f88d96f46..9b2b292a3 100644 --- a/src/libs/vwidgets/vabstractmainwindow.h +++ b/src/libs/vwidgets/vabstractmainwindow.h @@ -57,6 +57,9 @@ protected slots: protected: int m_curFileFormatVersion; QString m_curFileFormatVersionStr; + enum { MaxRecentFiles = 5 }; + QVector m_recentFileActs{QVector(MaxRecentFiles)}; + QAction *m_separatorAct{nullptr}; bool ContinueFormatRewrite(const QString ¤tFormatVersion, const QString &maxFormatVersion); void ToolBarStyle(QToolBar *bar); @@ -65,6 +68,9 @@ protected: virtual void ExportToCSVData(const QString &fileName, bool withHeader, int mib, const QChar &separator)=0; + virtual QStringList RecentFileList() const =0; + void UpdateRecentFileActions(); + private: Q_DISABLE_COPY(VAbstractMainWindow) };