From 758b7bda94c4af81bbfc4e884c22db974a7c12c0 Mon Sep 17 00:00:00 2001 From: Michele Calgaro Date: Thu, 10 Mar 2016 15:46:16 +0100 Subject: Kate session panel: added save and read-only functionality. Fixed up toolbar buttons status update. Added ability to activate a session using the ENTER key. Some other code refactoring. Signed-off-by: Michele Calgaro --- kate/app/katesession.cpp | 51 +++++++++++++----- kate/app/katesession.h | 51 ++++++++++++++++-- kate/app/katesessionpanel.cpp | 123 +++++++++++++++++++++++++++++++++++++++--- kate/app/katesessionpanel.h | 2 +- kate/app/kateviewspace.cpp | 2 +- 5 files changed, 200 insertions(+), 29 deletions(-) (limited to 'kate') diff --git a/kate/app/katesession.cpp b/kate/app/katesession.cpp index 17218d2b1..1ab43c420 100644 --- a/kate/app/katesession.cpp +++ b/kate/app/katesession.cpp @@ -91,7 +91,6 @@ KateSession::KateSession(const TQString &sessionName, const TQString &filename, } // Read only m_readOnly = m_config->readBoolEntry(KS_READONLY, false); - m_config->setReadOnly(m_readOnly); // Document list if (m_config->hasGroup(KS_DOCLIST)) { @@ -149,19 +148,9 @@ void KateSession::setSessionName(const TQString &sessionName) } //------------------------------------ -void KateSession::setReadOnly(bool readOnly) +void KateSession::save(bool saveGUIInfo, bool setReadOnly) { - m_readOnly = readOnly; - if (m_config) - { - m_config->setReadOnly(m_readOnly); - } -} - -//------------------------------------ -void KateSession::save(bool saveGUIInfo) -{ - if (m_readOnly) + if (m_readOnly && !setReadOnly) { return; } @@ -349,7 +338,6 @@ KateSessionManager::~KateSessionManager() // FIXME An option need to be added to Configure Kate -> Sessions to allow Kate to ask about // saving unnamed sessions before closing the current session. Default value is off as per // point above. - void KateSessionManager::saveConfig() { if (!m_config) @@ -372,6 +360,17 @@ void KateSessionManager::saveConfig() m_config->sync(); } +//------------------------------------ +KateSession* KateSessionManager::getSessionFromId(int sessionId) +{ + if (sessionId < 0 || sessionId >= (int)m_sessions.count()) + { + return NULL; + } + + return m_sessions[sessionId]; +} + //------------------------------------ int KateSessionManager::getSessionIdFromName(const TQString &name) { @@ -447,6 +446,17 @@ bool KateSessionManager::restoreLastSession() return activateSession(m_activeSessionId, false); } +//------------------------------------------- +void KateSessionManager::saveSession(int sessionId) +{ + if (sessionId < 0 || sessionId >= (int)m_sessions.count()) + { + return; + } + m_sessions[sessionId]->save(sessionId == m_activeSessionId); + emit sessionSaved(sessionId); +} + //------------------------------------------- bool KateSessionManager::deleteSession(int sessionId) { @@ -551,6 +561,19 @@ void KateSessionManager::renameSession(int sessionId, const TQString &newSession m_sessions[sessionId]->setSessionName(newSessionName); } + +//------------------------------------------- +void KateSessionManager::setSessionReadOnlyStatus(int sessionId, bool readOnly) +{ + if (sessionId < 0 || sessionId >= (int)m_sessions.count()) + { + return; + } + + m_sessions[sessionId]->setReadOnly(readOnly); + // Session is saved one last time when making it read only + m_sessions[sessionId]->save(sessionId == m_activeSessionId, true); +} //END KateSessionManager diff --git a/kate/app/katesession.h b/kate/app/katesession.h index 077ad1fda..3351db150 100644 --- a/kate/app/katesession.h +++ b/kate/app/katesession.h @@ -42,6 +42,9 @@ class TQCheckBox; //BEGIN KateSession +/** + * An object representing a Kate's session. + */ class KateSession { public: @@ -65,6 +68,7 @@ class KateSession * @return the session name */ const TQString& getSessionName() const { return m_sessionName; } + /** * Set the new session name * @param sessionName the new session name @@ -75,11 +79,12 @@ class KateSession * @return whether the session is read only or not */ bool isReadOnly() const { return m_readOnly; } + /** * Set session read only status - * @param readOnly if true, the session config can not be saved to file + * @param readOnly if true, the session status can not be modified */ - void setReadOnly(bool readOnly); + void setReadOnly(bool readOnly) { m_readOnly = readOnly; } /** * @return the session filename if available, otherwise the null string @@ -94,21 +99,24 @@ class KateSession /** * Save session info * @param saveGUIInfo if true, save also the information about the GUI elements + * @param setReadOnly necessary to save a session that has to be turned to read only */ - void save(bool saveGUIInfo); + void save(bool saveGUIInfo, bool setReadOnly = false); /** * Activate the session */ void activate(); + private: friend class KateViewSpace; + /** * @return the session config object */ - TDEConfig* getConfig() { return m_config; } + TDEConfig* getConfig() const { return m_config; } TQString m_sessionName; @@ -132,6 +140,12 @@ class KateSession //This would allow a safe use of multiple Kate instances without overwriting session information //among them. Currently the last instance to be closed will overwrite the information previously //saved by other Kate instances. +/** + * The Kate session manager. It takes care of storing and retrieving each session object + * as well as providing methods to operate on them. + * + * @note The Kate session manager takes ownership of each session object it handles. + */ class KateSessionManager : public TQObject { Q_OBJECT @@ -174,6 +188,12 @@ class KateSessionManager : public TQObject */ KateSession* getActiveSession() { return m_sessions[m_activeSessionId]; } + /** + * @param sessionId the id of the session to return + * @return a reference to the specified session + */ + KateSession* getSessionFromId(int sessionId); + /** * Return the session id of the first session whose name matches the * provided one. In case multiple sessions share the same name, @@ -216,8 +236,16 @@ class KateSessionManager : public TQObject /** * Saves the active session + * @emit sessionSaved (through invoked "void saveSession(int)" method) */ - void saveActiveSession() { m_sessions[m_activeSessionId]->save(true); } + void saveActiveSession() { saveSession(m_activeSessionId); } + + /** + * Save the specified session + * @param sessionId the id of the session to save + * @emit sessionSaved + */ + void saveSession(int sessionId); /** * Delete the specified session @@ -246,6 +274,13 @@ class KateSessionManager : public TQObject */ void renameSession(int sessionId, const TQString &newSessionName); + /** + * Set the read only status of the specified session + * @param sessionId the id of the session to modify + * @param readOnly the new read only status + */ + void setSessionReadOnlyStatus(int sessionId, bool readOnly); + signals: /** @@ -261,6 +296,12 @@ class KateSessionManager : public TQObject */ void sessionCreated(int sessionId); + /** + * Emitted once a session has been saved + * @param sessionId the id of the saved session + */ + void sessionSaved(int sessionId); + /** * Emitted once a session has been deleted * @param sessionId the id of the deleted session diff --git a/kate/app/katesessionpanel.cpp b/kate/app/katesessionpanel.cpp index 3a579dff4..b3f7ef55b 100644 --- a/kate/app/katesessionpanel.cpp +++ b/kate/app/katesessionpanel.cpp @@ -136,8 +136,13 @@ KateSessionPanel::KateSessionPanel(KateMainWindow *mainWindow, KateViewManager * m_listview->setSorting(-1); m_listview->setResizeMode(TQListView::LastColumn); //m_listview->setRootIsDecorated(true); // FIXME to enable after inserting doc list + + connect(m_listview, TQT_SIGNAL(selectionChanged()), + this, TQT_SLOT(slotSelectionChanged())); connect(m_listview, TQT_SIGNAL(executed(TQListViewItem*)), this, TQT_SLOT(slotItemExecuted(TQListViewItem*))); + connect(m_listview, TQT_SIGNAL(returnPressed(TQListViewItem*)), + this, TQT_SLOT(slotItemExecuted(TQListViewItem*))); connect(m_sessionManager, TQT_SIGNAL(sessionActivated(int, int)), this, TQT_SLOT(slotSessionActivated(int, int))); connect(m_sessionManager, TQT_SIGNAL(sessionCreated(int)), @@ -175,7 +180,6 @@ void KateSessionPanel::setup_toolbar() m_toolbar->setIconSize(16); m_toolbar->setEnableContextMenu(false); -//FIXME : uncomment and activate as long as the new session manager gets fixed // Toolbar actions TDEAction *a; @@ -183,7 +187,7 @@ void KateSessionPanel::setup_toolbar() TQT_TQOBJECT(this), TQT_SLOT(slotNewSession()), m_actionCollection, "session_new"); a->setWhatsThis(i18n("Create a new session.")); a->plug(m_toolbar); -/* + a = new TDEAction(i18n("Save"), SmallIcon("document-save"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotSaveSession()), m_actionCollection, "session_save"); a->setWhatsThis(i18n("Save the current session.")); @@ -193,7 +197,7 @@ void KateSessionPanel::setup_toolbar() TQT_TQOBJECT(this), TQT_SLOT(slotSaveSessionAs()), m_actionCollection, "session_save_as"); a->setWhatsThis(i18n("Save the current session with a different name.")); a->plug(m_toolbar); -*/ + a = new TDEAction(i18n("Rename"), SmallIcon("edit_user"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotRenameSession()), m_actionCollection, "session_rename"); a->setWhatsThis(i18n("Rename the selected session.")); @@ -210,7 +214,7 @@ void KateSessionPanel::setup_toolbar() TQT_TQOBJECT(this), TQT_SLOT(slotActivateSession()), m_actionCollection, "session_activate"); a->setWhatsThis(i18n("Activate the selected session.")); a->plug(m_toolbar); -/* + TDEToggleAction *tglA = new TDEToggleAction(i18n("Toggle read only"), SmallIcon("encrypted"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotSessionToggleReadOnly()), m_actionCollection, "session_toggle_read_only"); tglA->setWhatsThis(i18n("Toggle read only status for the selected session.

" @@ -218,7 +222,6 @@ void KateSessionPanel::setup_toolbar() "will not be saved when you exit Kate or switch to another session.

" "You can use this option to create template sessions that you wish to keep unchanged over time.")); tglA->plug(m_toolbar); -*/ a = new TDEAction(i18n("Move Up"), SmallIcon("go-up"), 0, TQT_TQOBJECT(this), TQT_SLOT(slotSessionMoveUp()), m_actionCollection, "session_move_up"); @@ -229,6 +232,8 @@ void KateSessionPanel::setup_toolbar() TQT_TQOBJECT(this), TQT_SLOT(slotSessionMoveDown()), m_actionCollection, "session_move_down"); a->setWhatsThis(i18n("Move down the selected session.")); a->plug(m_toolbar); + + //FIXME add button to restore a modified session to its original if not yet saved to disk } //------------------------------------------- @@ -246,13 +251,36 @@ void KateSessionPanel::slotNewSession() //------------------------------------------- void KateSessionPanel::slotSaveSession() { -//TODO + KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); + if (!sessionItem) + { + return; + } + + int sessId = sessionItem->getSessionId(); + const KateSession *ks = m_sessionManager->getSessionFromId(sessId); + if (!ks) + { + return; + } + + if (ks->getSessionFilename().isEmpty()) + { + // Session has never been saved before. Ask user for a session name first + slotSaveSessionAs(); + } + else + { + m_sessionManager->saveSession(sessId); + slotSelectionChanged(); // Update the toolbar button status + } } //------------------------------------------- void KateSessionPanel::slotSaveSessionAs() { //TODO + slotSelectionChanged(); // Update the toolbar button status } //------------------------------------------- @@ -260,7 +288,9 @@ void KateSessionPanel::slotRenameSession() { TQListViewItem *sessionItem = m_listview->selectedItem(); if (!sessionItem) + { return; + } m_listview->rename(m_listview->selectedItem(), m_columnName); } @@ -270,7 +300,9 @@ void KateSessionPanel::slotDeleteSession() { KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); if (!sessionItem) + { return; + } int result = KMessageBox::warningContinueCancel(this, i18n("Do you really want to delete the session '%1'?").arg(sessionItem->text(0)), @@ -286,7 +318,9 @@ void KateSessionPanel::slotActivateSession() { KateSessionPanelItem *newSessionItem = dynamic_cast(m_listview->selectedItem()); if (!newSessionItem) + { return; + } int currSessionId = m_sessionManager->getActiveSessionId(); int newSessionId = newSessionItem->getSessionId(); @@ -299,7 +333,16 @@ void KateSessionPanel::slotActivateSession() //------------------------------------------- void KateSessionPanel::slotSessionToggleReadOnly() { -//TODO + KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); + if (!sessionItem) + { + return; + } + + m_sessionManager->setSessionReadOnlyStatus(sessionItem->getSessionId(), + (dynamic_cast( + m_actionCollection->action("session_toggle_read_only")))->isChecked()); + slotSelectionChanged(); // Update the toolbar button status } //------------------------------------------- @@ -307,7 +350,9 @@ void KateSessionPanel::slotSessionMoveUp() { KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); if (!sessionItem) + { return; + } m_sessionManager->moveSessionBackward(sessionItem->getSessionId()); } @@ -317,15 +362,20 @@ void KateSessionPanel::slotSessionMoveDown() { KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); if (!sessionItem) + { return; + } m_sessionManager->moveSessionForward(sessionItem->getSessionId()); } +//------------------------------------------- void KateSessionPanel::slotItemExecuted(TQListViewItem *item) { if (!item) - return; + { + return; + } // First level items are sessions. Executing one, will switch to that session if (!item->parent()) @@ -335,6 +385,62 @@ void KateSessionPanel::slotItemExecuted(TQListViewItem *item) } } +//------------------------------------------- +void KateSessionPanel::slotSelectionChanged() +{ + KateSessionPanelItem *sessionItem = dynamic_cast(m_listview->selectedItem()); + const KateSession *ks(NULL); + if (sessionItem) + { + ks = m_sessionManager->getSessionFromId(sessionItem->getSessionId()); + } + + TDEToggleAction *readOnlyAction = dynamic_cast( + m_actionCollection->action("session_toggle_read_only")); + if (!sessionItem || !ks) + { + m_actionCollection->action("session_save")->setEnabled(false); + m_actionCollection->action("session_save_as")->setEnabled(false); + m_actionCollection->action("session_rename")->setEnabled(false); + m_actionCollection->action("session_delete")->setEnabled(false); + m_actionCollection->action("session_activate")->setEnabled(false); + m_actionCollection->action("session_move_up")->setEnabled(false); + m_actionCollection->action("session_move_down")->setEnabled(false); + readOnlyAction->setEnabled(false); + readOnlyAction->setChecked(false); + } + else + { + if (ks->isReadOnly()) + { + // Read only sessions can not be saved or renamed + m_actionCollection->action("session_save")->setEnabled(false); + m_actionCollection->action("session_rename")->setEnabled(false); + } + else + { + m_actionCollection->action("session_save")->setEnabled(true); + m_actionCollection->action("session_rename")->setEnabled(true); + } + if (ks->getSessionFilename().isEmpty()) + { + // Unstored sessions can not be made readonly + readOnlyAction->setEnabled(false); + readOnlyAction->setChecked(false); + } + else + { + readOnlyAction->setEnabled(true); + readOnlyAction->setChecked(ks->isReadOnly()); + } + m_actionCollection->action("session_save_as")->setEnabled(true); + m_actionCollection->action("session_delete")->setEnabled(true); + m_actionCollection->action("session_activate")->setEnabled(true); + m_actionCollection->action("session_move_up")->setEnabled(true); + m_actionCollection->action("session_move_down")->setEnabled(true); + } +} + //------------------------------------------- void KateSessionPanel::slotSessionActivated(int newSessionId, int oldSessionId) { @@ -458,6 +564,7 @@ void KateSessionPanel::slotSessionRenamed(TQListViewItem *item) { return; } + m_sessionManager->renameSession(sessionItem->getSessionId(), sessionItem->text(m_columnName)); } //END KateSessionPanel diff --git a/kate/app/katesessionpanel.h b/kate/app/katesessionpanel.h index 1010a4e13..265d1d8e1 100644 --- a/kate/app/katesessionpanel.h +++ b/kate/app/katesessionpanel.h @@ -127,13 +127,13 @@ class KateSessionPanel : public TQVBox void slotSessionMoveDown(); void slotItemExecuted(TQListViewItem *item); + void slotSelectionChanged(); void slotSessionActivated(int newSessionId, int oldSessionId); void slotSessionCreated(int sessionId); void slotSessionDeleted(int sessionId); void slotSessionsSwapped(int sessionIdMin, int sessionIdMax); void slotSessionRenamed(TQListViewItem *item); - private: void setup_toolbar(); diff --git a/kate/app/kateviewspace.cpp b/kate/app/kateviewspace.cpp index 7e9622b67..a4066ee5a 100644 --- a/kate/app/kateviewspace.cpp +++ b/kate/app/kateviewspace.cpp @@ -114,7 +114,7 @@ void KateViewSpace::addView(Kate::View* v, bool show) { TQString vgroup = TQString("%1 %2").arg(m_group).arg(fn); - KateSession *as = KateSessionManager::self()->getActiveSession(); + const KateSession *as = KateSessionManager::self()->getActiveSession(); TDEConfig *asCfg = as->getConfig(); if (asCfg && asCfg->hasGroup(vgroup)) { -- cgit v1.2.1