diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9bffe4f..9c7db88 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -91,7 +91,7 @@ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) : m_qTranslator(nullptr), m_uiGuiTranslator(nullptr), m_textEditLineColumnInfoLabel(nullptr), m_oldLinesNumber(0), m_encodingsList(), m_encodingActionGroup(nullptr), m_saveEncodedActionGroup(nullptr), m_highlighterActionGroup(nullptr), - m_documentModified(false) + m_documentModified(false), m_previewToggled(true) ///- _mainWindowForm(nullptr), _settings(nullptr) ///- m_highlighter(nullptr), _aboutDialogGraphicsView(nullptr), _settingsDialog(nullptr) ///- m_textEditVScrollBar(nullptr), _toolBarWidget(nullptr), _indentHandler(nullptr) @@ -244,9 +244,12 @@ void MainWindow::initMainWindow() connect(actionExportHTML, SIGNAL(activated()), this, SLOT(exportToHTML())); ///-- connect(actionShowLog, SIGNAL(activated()), ///-- debugging::TSLogger::getInstance(), SLOT(show())); -///-- + // Init the menu for selecting one of the recently opened files. + initRecentlyOpenedList(); updateRecentlyOpenedList(); + connect(popupMenuRecentlyOpenedFiles, SIGNAL(highlighted(int)), + this, SLOT(recentlyOpenedFileHighlighted(int))); connect(popupMenuRecentlyOpenedFiles, SIGNAL(activated(int)), this, SLOT(openFileFromRecentlyOpenedList(int))); ///-- //connect( _settings, SIGNAL(recentlyOpenedListSize(int)), this, SLOT(updateRecentlyOpenedList()) @@ -1104,6 +1107,9 @@ void MainWindow::saveSettings() m_settings->setValueByName("WindowSize", size()); } m_settings->setValueByName("FileEncoding", m_currentEncoding); + + m_settings->setValueByName("LastOpenedFiles", m_recentlyOpenedList.join("|")); + ///-- m_settings->setValueByName("MainWindowState", saveState()); ///-- ///-- // Also save the syntax highlight style for all lexers. @@ -1432,100 +1438,87 @@ void MainWindow::numberOfLinesChanged() ///-- } ///-- } +/* + \brief Initialize the list of recently opened files and + the menu using the saved settings. + */ +void MainWindow::initRecentlyOpenedList() +{ + m_recentlyOpenedListMaxSize = m_settings->getValueByName("RecentlyOpenedListSize").toInt(); + TQString lastOpenedFilesString = m_settings->getValueByName("LastOpenedFiles").toString(); + TQStringList recentlyOpenedList = TQStringList::split("|", lastOpenedFilesString); + + // Append only existing files and as long as the max limit has not been reached + int index = 0; + for (const TQString &file : recentlyOpenedList) + { + TQFileInfo fileInfo(file); + if (fileInfo.exists()) + { + if (index < m_recentlyOpenedListMaxSize) + { + m_recentlyOpenedList.append(file); + int id = popupMenuRecentlyOpenedFiles->insertItem(fileInfo.fileName(), -1, index); + popupMenuRecentlyOpenedFiles->setWhatsThis(id, file); + ++index; + } + else + { + break; + } + } + } + + // Enable or disable "actionClearRecentlyOpenedList" + actionClearRecentlyOpenedList->setEnabled(!m_recentlyOpenedList.isEmpty()); +} + /* \brief Updates the list of recently opened files. - Therefore the currently open file is set at the lists first position - regarding the in the _settings set maximum list length. Overheads of the - list will be cut off. The new list will be updated to the _settings and + The currently open file is set at the top of the list. + Files in excess of maximum list length will be discarded. + The new list will be updated into the m_settings object and the recently opened menu will be updated too. */ void MainWindow::updateRecentlyOpenedList() { -///-- TQString fileName; -///-- TQString filePath; -///-- TQStringList recentlyOpenedList = -///-- _settings->getValueByName("lastSourceCodeFile").toString().split("|"); -///-- TQList recentlyOpenedActionList = -///-- _mainWindowForm->menuRecently_Opened_Files->actions(); -///-- -///-- // Check if the currently open file is in the list of recently opened. -///-- int indexOfCurrentFile = recentlyOpenedList.indexOf(m_currentSourceFile); -///-- -///-- // If it is in the list of recently opened files and not at the first position, move it to the -///-- // first pos. -///-- if (indexOfCurrentFile > 0) -///-- { -///-- recentlyOpenedList.move(indexOfCurrentFile, 0); -///-- recentlyOpenedActionList.move(indexOfCurrentFile, 0); -///-- } -///-- // Put the current file at the first position if it not already is and is not empty. -///-- else if (indexOfCurrentFile == -1 && !m_currentSourceFile.isEmpty()) -///-- { -///-- recentlyOpenedList.insert(0, m_currentSourceFile); -///-- TQAction *recentlyOpenedAction = new TQAction(TQFileInfo( -///-- m_currentSourceFile).fileName(), _mainWindowForm->menuRecently_Opened_Files); -///-- recentlyOpenedAction->setStatusTip(m_currentSourceFile); -///-- recentlyOpenedActionList.insert(0, recentlyOpenedAction); -///-- } -///-- -///-- // Get the maximum recently opened list size. -///-- int recentlyOpenedListMaxSize = _settings->getValueByName("recentlyOpenedListSize").toInt(); -///-- -///-- // Loop for each filepath in the recently opened list, remove non existing files and -///-- // loop only as long as maximum allowed list entries are set. -///-- for (int i = 0; i < recentlyOpenedList.size() && i < recentlyOpenedListMaxSize; ) -///-- { -///-- filePath = recentlyOpenedList.at(i); -///-- TQFileInfo fileInfo(filePath); -///-- -///-- // If the file does no longer exist, remove it from the list. -///-- if (!fileInfo.exists()) -///-- { -///-- recentlyOpenedList.takeAt(i); -///-- if (i < recentlyOpenedActionList.size() - 2) -///-- { -///-- TQAction *action = recentlyOpenedActionList.takeAt(i); -///-- delete action; -///-- } -///-- } -///-- // else if its not already in the menu, add it to the menu. -///-- else -///-- { -///-- if (i >= recentlyOpenedActionList.size() - 2) -///-- { -///-- TQAction *recentlyOpenedAction = new TQAction( -///-- fileInfo.fileName(), _mainWindowForm->menuRecently_Opened_Files); -///-- recentlyOpenedAction->setStatusTip(filePath); -///-- recentlyOpenedActionList.insert(recentlyOpenedActionList.size() - 2, recentlyOpenedAction); -///-- } -///-- i++; -///-- } -///-- } -///-- -///-- // Trim the list to its in the _settings allowed maximum size. -///-- while (recentlyOpenedList.size() > recentlyOpenedListMaxSize) -///-- { -///-- recentlyOpenedList.takeLast(); -///-- TQAction *action = recentlyOpenedActionList.takeAt(recentlyOpenedActionList.size() - 3); -///-- delete action; -///-- } -///-- -///-- // Add all actions to the menu. -///-- _mainWindowForm->menuRecently_Opened_Files->addActions(recentlyOpenedActionList); -///-- _mainWindowForm->menuRecently_Opened_Files->addActions(recentlyOpenedActionList); -///-- -///-- // Write the new recently opened list to the _settings. -///-- _settings->setValueByName("lastSourceCodeFile", recentlyOpenedList.join("|")); -///-- -///-- // Enable or disable "actionClearRecentlyOpenedList" if list is [not] emtpy -///-- if (recentlyOpenedList.isEmpty()) -///-- { -///-- _mainWindowForm->actionClearRecentlyOpenedList->setEnabled(false); -///-- } -///-- else -///-- { -///-- _mainWindowForm->actionClearRecentlyOpenedList->setEnabled(true); -///-- } + TQString fileName; + TQString filePath; + + // Check if the currently open file is in the list of recently opened. + int currentFileIndex = m_recentlyOpenedList.findIndex(m_currentSourceFile); + if (!m_currentSourceFile.isEmpty()) + { + // If it is in the list of recently opened files and not at the first position, + // move it to the first pos. + if (currentFileIndex > 0) + { + TQStringList::iterator currentFileIt = m_recentlyOpenedList.find(m_currentSourceFile); + m_recentlyOpenedList.remove(currentFileIt); + m_recentlyOpenedList.prepend(m_currentSourceFile); + popupMenuRecentlyOpenedFiles->removeItemAt(currentFileIndex); + int id = popupMenuRecentlyOpenedFiles->insertItem(TQFileInfo(m_currentSourceFile).fileName(), + -1, 0); + popupMenuRecentlyOpenedFiles->setWhatsThis(id, m_currentSourceFile); + } + // Put the current file at the first position if it not already is and is not empty. + else if (currentFileIndex == -1) + { + m_recentlyOpenedList.prepend(m_currentSourceFile); + int id = popupMenuRecentlyOpenedFiles->insertItem(TQFileInfo(m_currentSourceFile).fileName(), + -1, 0); + popupMenuRecentlyOpenedFiles->setWhatsThis(id, m_currentSourceFile); + // Trim the last element if we have reached the max allowed size + if (m_recentlyOpenedList.size() > m_recentlyOpenedListMaxSize) + { + m_recentlyOpenedList.pop_back(); + popupMenuRecentlyOpenedFiles->removeItemAt(m_recentlyOpenedListMaxSize); + } + } + } + + // Enable or disable "actionClearRecentlyOpenedList" + actionClearRecentlyOpenedList->setEnabled(!m_recentlyOpenedList.isEmpty()); } /* @@ -1533,23 +1526,24 @@ void MainWindow::updateRecentlyOpenedList() */ void MainWindow::clearRecentlyOpenedList() { -///-- TQStringList recentlyOpenedList = -///-- _settings->getValueByName("lastSourceCodeFile").toString().split("|"); -///-- TQList recentlyOpenedActionList = -///-- _mainWindowForm->menuRecently_Opened_Files->actions(); -///-- -///-- while (recentlyOpenedList.size() > 0) -///-- { -///-- recentlyOpenedList.takeLast(); -///-- TQAction *action = recentlyOpenedActionList.takeAt(recentlyOpenedActionList.size() - 3); -///-- delete action; -///-- } -///-- -///-- // Write the new recently opened list to the _settings. -///-- _settings->setValueByName("lastSourceCodeFile", recentlyOpenedList.join("|")); -///-- -///-- // Disable "actionClearRecentlyOpenedList" -///-- _mainWindowForm->actionClearRecentlyOpenedList->setEnabled(false); + int numItems = m_recentlyOpenedList.size(); + m_recentlyOpenedList.clear(); + for (int i = 0; i < numItems; ++i) + { + popupMenuRecentlyOpenedFiles->removeItemAt(0); + } + + // Disable "actionClearRecentlyOpenedList" + actionClearRecentlyOpenedList->setEnabled(false); +} + +/* + \brief When a recent file action is highlighted, set the full path + in the status bar + */ +void MainWindow::recentlyOpenedFileHighlighted(int recentlyOpenedActionId) +{ + statusBar()->message(popupMenuRecentlyOpenedFiles->whatsThis(recentlyOpenedActionId)); } /* @@ -1566,31 +1560,30 @@ void MainWindow::openFileFromRecentlyOpenedList(int recentlyOpenedActionId) return; } -///-- TQString fileName = recentlyOpenedAction->text(); -///-- int indexOfSelectedFile = _mainWindowForm->menuRecently_Opened_Files->actions().indexOf( -///-- recentlyOpenedAction); -///-- TQStringList recentlyOpenedList = -///-- _settings->getValueByName("lastSourceCodeFile").toString().split("|"); -///-- TQString filePath = recentlyOpenedList.at(indexOfSelectedFile); -///-- TQFileInfo fileInfo(filePath); -///-- -///-- // If the file exists, open it. -///-- if (fileInfo.exists()) -///-- { -///-- openSourceFileDialog(filePath); -///-- } -///-- // If it does not exist, show a warning message and update the list of recently opened files. -///-- else -///-- { -///-- TQMessageBox::warning(NULL, tr("File no longer exists"), -///-- tr("The file %1 in the list of recently opened files does no longer exist.")); -///-- // The function updateRecentlyOpenedList() has to be called via a singleShot so it is executed -///-- // after this -///-- // function (openFileFromRecentlyOpenedList) has already been left. This has to be done because -///-- // a TQt3Support function tries to emit a signal based on the existing actions and deleting -///-- // any of these actions in updateRecentlyOpenedList() causes an error. -///-- TQTimer::singleShot(0, this, SLOT(updateRecentlyOpenedList())); -///-- } + TQString filePath = popupMenuRecentlyOpenedFiles->whatsThis(recentlyOpenedActionId); + TQFileInfo fileInfo(filePath); + + if (fileInfo.exists()) + { + // If the file exists, open it. + openSourceFileDialog(filePath); + } + else + { + // If it does not exist, show a warning message and update the list of recently opened files. + TQMessageBox::warning(this, tr("File no longer exists"), + tr("The file %1 no longer exists.").arg(filePath)); + int fileIndex = m_recentlyOpenedList.findIndex(filePath); + if (fileIndex >= 0) + { + TQStringList::iterator fileIt = m_recentlyOpenedList.find(filePath); + m_recentlyOpenedList.remove(fileIt); + popupMenuRecentlyOpenedFiles->removeItemAt(fileIndex); + + // Enable or disable "actionClearRecentlyOpenedList" + actionClearRecentlyOpenedList->setEnabled(!m_recentlyOpenedList.isEmpty()); + } + } } ///-- /* diff --git a/src/MainWindow.h b/src/MainWindow.h index 340d9ed..4d2fe2f 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -71,6 +71,7 @@ class MainWindow : public MainWindowBase void encodingChanged(TQAction *encodingAction); void numberOfLinesChanged(); void updateRecentlyOpenedList(); + void recentlyOpenedFileHighlighted(int recentlyOpenedActionId); void openFileFromRecentlyOpenedList(int recentlyOpenedActionId); void clearRecentlyOpenedList(); void showAboutDialog(); @@ -91,6 +92,7 @@ class MainWindow : public MainWindowBase void initTextEditor(); void initSyntaxHighlighter(); void initIndenter(); + void initRecentlyOpenedList(); ///-- void changeEvent(TQEvent *event); ///-- void dragEnterEvent(TQDragEnterEvent *event); ///-- void dropEvent(TQDropEvent *event); @@ -108,6 +110,7 @@ class MainWindow : public MainWindowBase ///-- AboutDialogGraphicsView *_aboutDialogGraphicsView; ///-- UiGuiSettingsDialog *_settingsDialog; int m_actionClearRecentlyOpenedListId; + int m_recentlyOpenedListMaxSize; ///-- int _textEditLastScrollPos; ///-- int _currentIndenterID; int m_oldLinesNumber; @@ -126,6 +129,7 @@ class MainWindow : public MainWindowBase bool m_previewToggled; bool m_documentModified; TQStringList m_encodingsList; + TQStringList m_recentlyOpenedList; ToolBarWidget *m_toolBarWidget; ///-- IndentHandler *_indentHandler;