19 #include "katesessionpanel.h"
20 #include "katesessionpanel.moc"
23 #include "katemainwindow.h"
24 #include "kateviewmanager.h"
25 #include "katesession.h"
27 #include <kiconloader.h>
28 #include <tdelocale.h>
29 #include <tdemessagebox.h>
30 #include <tqlistview.h>
36 const char *KS_UNNAMED =
"Unnamed";
42 KateSessionNameChooser::KateSessionNameChooser(TQWidget *parent,
bool showSwitchTo)
43 : KDialogBase(parent,
"", true, i18n(
"Session Name Chooser"), KDialogBase::User1 | KDialogBase::User2,
44 KDialogBase::User2, true, KStdGuiItem::cancel(), KGuiItem(i18n(
"Continue"),
"document-new")),
45 m_showSwitchTo(showSwitchTo)
47 TQHBox *page =
new TQHBox(
this);
51 TQVBox *vb =
new TQVBox(page);
52 vb->setSpacing(KDialog::spacingHint());
54 TQLabel *label =
new TQLabel(vb);
55 label->setText(
"Please type the new session name:");
57 m_sessionNameLE =
new TQLineEdit(vb);
58 m_sessionNameLE->setText(i18n(KS_UNNAMED));
59 m_sessionNameLE->setFocus();
63 m_activateCB =
new TQCheckBox(i18n(
"Switch to the new session"), vb, NULL);
64 m_activateCB->setChecked(
true);
67 connect(m_sessionNameLE, TQ_SIGNAL(textChanged(
const TQString&)),
this, TQ_SLOT(slotTextChanged()));
72 TQString KateSessionNameChooser::getSessionName()
74 return m_sessionNameLE->text();
78 bool KateSessionNameChooser::getActivateFlag()
82 return m_activateCB->isChecked();
88 void KateSessionNameChooser::slotUser1()
94 void KateSessionNameChooser::slotUser2()
100 void KateSessionNameChooser::slotTextChanged()
102 enableButton(KDialogBase::User2, !m_sessionNameLE->text().isEmpty());
108 void KateSessionPanelToolBarParent::setToolBar(TDEToolBar *tbar)
114 void KateSessionPanelToolBarParent::resizeEvent (TQResizeEvent*)
118 setMinimumHeight(m_tbar->sizeHint().height());
119 m_tbar->resize(width(),height());
127 KateSessionPanel::KateSessionPanel(KateMainWindow *mainWindow, KateViewManager *viewManager,
128 TQWidget *parent,
const char *name)
130 m_actionCollection(new TDEActionCollection(this)), m_columnName(-1), m_columnPixmap(-1)
136 m_listview =
new TDEListView(
this);
137 m_listview->header()->hide();
138 m_columnName = m_listview->addColumn(
"Session name");
139 m_columnPixmap = m_listview->addColumn(
"Pixmap", 24);
140 m_listview->addColumn(
"Dummy", 1);
141 m_listview->header()->setResizeEnabled(
false, m_columnPixmap);
142 m_listview->setColumnAlignment(m_columnPixmap, TQt::AlignCenter);
143 m_listview->setMinimumWidth(m_listview->sizeHint().width());
144 m_listview->setSorting(-1);
145 m_listview->setResizeMode(TQListView::LastColumn);
149 connect(m_listview, TQ_SIGNAL(selectionChanged()),
150 this, TQ_SLOT(slotSelectionChanged()));
151 connect(m_listview, TQ_SIGNAL(executed(TQListViewItem*)),
152 this, TQ_SLOT(slotItemExecuted(TQListViewItem*)));
153 connect(m_listview, TQ_SIGNAL(returnPressed(TQListViewItem*)),
154 this, TQ_SLOT(slotItemExecuted(TQListViewItem*)));
156 this, TQ_SLOT(slotSelectionChanged()));
157 connect(m_sessionManager, TQ_SIGNAL(switchOptionChanged()),
158 this, TQ_SLOT(slotSelectionChanged()));
159 connect(m_sessionManager, TQ_SIGNAL(sessionActivated(
int,
int)),
160 this, TQ_SLOT(slotSessionActivated(
int,
int)));
161 connect(m_sessionManager, TQ_SIGNAL(sessionCreated(
int)),
162 this, TQ_SLOT(slotSessionCreated(
int)));
163 connect(m_sessionManager, TQ_SIGNAL(sessionDeleted(
int)),
164 this, TQ_SLOT(slotSessionDeleted(
int)));
165 connect(m_sessionManager, TQ_SIGNAL(sessionsSwapped(
int,
int)),
166 this, TQ_SLOT(slotSessionsSwapped(
int,
int)));
167 connect(m_sessionManager, TQ_SIGNAL(sessionRenamed(
int)),
168 this, TQ_SLOT(slotSessionRenamed(
int)));
169 connect(m_listview, TQ_SIGNAL(itemRenamed(TQListViewItem*)),
170 this, TQ_SLOT(slotLVSessionRenamed(TQListViewItem*)));
172 TQPtrList<KateSession>& sessions = m_sessionManager->getSessionsList();
173 for (
int idx = sessions.count() - 1; idx >= 0; --idx)
175 new KateSessionPanelItem(m_listview, sessions[idx]->getSessionName(), idx);
176 if (idx == m_sessionManager->getActiveSessionId())
178 m_listview->setSelected(m_listview->firstChild(),
true);
179 m_listview->firstChild()->setPixmap(m_columnPixmap, SmallIcon(
"ok"));
186 void KateSessionPanel::setup_toolbar()
189 KateSessionPanelToolBarParent *tbarParent =
new KateSessionPanelToolBarParent(
this);
190 m_toolbar =
new TDEToolBar(tbarParent,
"Kate Session Panel Toolbar",
true);
191 tbarParent->setToolBar(m_toolbar);
192 m_toolbar->setMovingEnabled(
false);
193 m_toolbar->setFlat(
true);
194 m_toolbar->setIconText(TDEToolBar::IconOnly);
195 m_toolbar->setIconSize(16);
196 m_toolbar->setEnableContextMenu(
false);
201 a =
new TDEAction(i18n(
"New"), SmallIcon(
"list-add"), 0,
202 this, TQ_SLOT(slotNewSession()), m_actionCollection,
"session_new");
203 a->setWhatsThis(i18n(
"Create a new session and switch to it."));
206 a =
new TDEAction(i18n(
"Save"), SmallIcon(
"document-save"), 0,
207 this, TQ_SLOT(slotSaveSession()), m_actionCollection,
"session_save");
208 a->setWhatsThis(i18n(
"Save the selected session."));
211 a =
new TDEAction(i18n(
"Save as..."), SmallIcon(
"document-save-as"), 0,
212 this, TQ_SLOT(slotSaveSessionAs()), m_actionCollection,
"session_save_as");
213 a->setWhatsThis(i18n(
"Save an unsaved session with a new name or clone an already saved session "
214 "into a new session."));
217 a =
new TDEAction(i18n(
"Rename"), SmallIcon(
"edit_user"), 0,
218 this, TQ_SLOT(slotRenameSession()), m_actionCollection,
"session_rename");
219 a->setWhatsThis(i18n(
"Rename the selected session."));
222 a =
new TDEAction(i18n(
"Delete"), SmallIcon(
"edit-delete"), 0,
223 this, TQ_SLOT(slotDeleteSession()), m_actionCollection,
"session_delete");
224 a->setWhatsThis(i18n(
"Delete the selected session."));
227 a =
new TDEAction(i18n(
"Reload"), SmallIcon(
"reload"), 0,
228 this, TQ_SLOT(slotReloadSession()), m_actionCollection,
"session_reload");
229 a->setWhatsThis(i18n(
"Reload the last saved state of the selected session."));
232 m_toolbar->insertLineSeparator();
234 a =
new TDEAction(i18n(
"Activate"), SmallIcon(
"forward"), 0,
235 this, TQ_SLOT(slotActivateSession()), m_actionCollection,
"session_activate");
236 a->setWhatsThis(i18n(
"Activate the selected session."));
239 TDEToggleAction *tglA =
new TDEToggleAction(i18n(
"Toggle read only"), SmallIcon(
"encrypted"), 0,
240 this, TQ_SLOT(slotSessionToggleReadOnly()), m_actionCollection,
"session_toggle_read_only");
241 tglA->setWhatsThis(i18n(
"Toggle read only status for the selected session.<p>"
242 "In a read only session, you can work as usual but the list of documents in the session "
243 "will not be saved when you exit Kate or switch to another session.<p>"
244 "You can use this option to create template sessions that you wish to keep unchanged over time."));
245 tglA->plug(m_toolbar);
247 a =
new TDEAction(i18n(
"Move Up"), SmallIcon(
"go-up"), 0,
248 this, TQ_SLOT(slotSessionMoveUp()), m_actionCollection,
"session_move_up");
249 a->setWhatsThis(i18n(
"Move up the selected session."));
252 a =
new TDEAction(i18n(
"Move Down"), SmallIcon(
"go-down"), 0,
253 this, TQ_SLOT(slotSessionMoveDown()), m_actionCollection,
"session_move_down");
254 a->setWhatsThis(i18n(
"Move down the selected session."));
259 void KateSessionPanel::slotNewSession()
261 KateSessionNameChooser *nameChooser =
new KateSessionNameChooser(
this,
false);
262 int result = nameChooser->exec();
263 if (result == TQDialog::Accepted)
265 int res = handleSessionSwitch();
266 if (res == KMessageBox::Cancel)
272 m_sessionManager->newSession(nameChooser->getSessionName(), res == KMessageBox::Yes);
278 void KateSessionPanel::slotSaveSession()
280 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
286 int sessId = sessionItem->getSessionId();
287 const KateSession *ks = m_sessionManager->getSessionFromId(sessId);
300 m_sessionManager->saveSession(sessId);
301 slotSelectionChanged();
306 void KateSessionPanel::slotSaveSessionAs()
308 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
313 int sessId = sessionItem->getSessionId();
314 KateSession *ks = m_sessionManager->getSessionFromId(sessId);
324 KateSessionNameChooser *nameChooser =
new KateSessionNameChooser(
this, cloneSession);
325 int result = nameChooser->exec();
326 if (result == TQDialog::Accepted)
331 m_sessionManager->renameSession(sessId, nameChooser->getSessionName());
332 m_sessionManager->saveSession(sessId);
337 bool activate = nameChooser->getActivateFlag();
338 int activeSessionId = m_sessionManager->getActiveSessionId();
339 int res = KMessageBox::Yes;
340 if (activate && sessId != activeSessionId)
344 res = handleSessionSwitch();
345 if (res == KMessageBox::Cancel)
350 m_sessionManager->cloneSession(sessId, nameChooser->getSessionName(), activate, res == KMessageBox::No);
354 slotSelectionChanged();
358 void KateSessionPanel::slotRenameSession()
360 TQListViewItem *sessionItem = m_listview->selectedItem();
366 m_listview->rename(m_listview->selectedItem(), m_columnName);
370 void KateSessionPanel::slotDeleteSession()
372 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
378 int result = KMessageBox::warningContinueCancel(
this,
379 i18n(
"Do you really want to delete the session \"%1\"?").arg(sessionItem->text(0)),
380 i18n(
"Delete session"), KStdGuiItem::del());
381 if (result == KMessageBox::Continue)
383 int sessionId = sessionItem->getSessionId();
384 if (sessionId == m_sessionManager->getActiveSessionId())
389 if (!
KateApp::self()->activeMainWindow()->queryClose_internal())
395 m_sessionManager->deleteSession(sessionId, KateSessionManager::INVALID_SESSION);
400 void KateSessionPanel::slotReloadSession()
402 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
407 int sessId = sessionItem->getSessionId();
408 if (sessId != m_sessionManager->getActiveSessionId())
414 m_sessionManager->reloadActiveSession();
418 void KateSessionPanel::slotActivateSession()
420 KateSessionPanelItem *newSessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
426 int currSessionId = m_sessionManager->getActiveSessionId();
427 int newSessionId = newSessionItem->getSessionId();
428 if (newSessionId != currSessionId)
430 int res = handleSessionSwitch();
431 if (res == KMessageBox::Cancel)
437 m_sessionManager->activateSession(newSessionId, res == KMessageBox::Yes);
443 void KateSessionPanel::slotSessionToggleReadOnly()
445 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
449 ks = m_sessionManager->getSessionFromId(sessionItem->getSessionId());
451 if (!sessionItem || !ks)
456 m_sessionManager->setSessionReadOnlyStatus(sessionItem->getSessionId(), !ks->
isReadOnly());
457 slotSelectionChanged();
461 void KateSessionPanel::slotSessionMoveUp()
463 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
469 m_sessionManager->moveSessionBackward(sessionItem->getSessionId());
473 void KateSessionPanel::slotSessionMoveDown()
475 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
481 m_sessionManager->moveSessionForward(sessionItem->getSessionId());
485 void KateSessionPanel::slotItemExecuted(TQListViewItem *item)
494 if (!item->parent() &&
495 m_actionCollection->action(
"session_activate")->isEnabled())
497 slotActivateSession();
503 void KateSessionPanel::slotSelectionChanged()
505 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(m_listview->selectedItem());
509 ks = m_sessionManager->getSessionFromId(sessionItem->getSessionId());
512 TDEToggleAction *readOnlyAction =
dynamic_cast<TDEToggleAction*
>(
513 m_actionCollection->action(
"session_toggle_read_only"));
514 if (!sessionItem || !ks ||
515 m_sessionManager->getSwitchOption() == KateSessionManager::SWITCH_DISCARD)
517 m_actionCollection->action(
"session_save")->setEnabled(
false);
518 m_actionCollection->action(
"session_save_as")->setEnabled(
false);
519 m_actionCollection->action(
"session_rename")->setEnabled(
false);
520 m_actionCollection->action(
"session_delete")->setEnabled(
false);
521 m_actionCollection->action(
"session_reload")->setEnabled(
false);
522 m_actionCollection->action(
"session_activate")->setEnabled(
false);
523 m_actionCollection->action(
"session_move_up")->setEnabled(
false);
524 m_actionCollection->action(
"session_move_down")->setEnabled(
false);
525 readOnlyAction->setEnabled(
false);
526 readOnlyAction->setChecked(
false);
533 m_actionCollection->action(
"session_save")->setEnabled(
false);
534 m_actionCollection->action(
"session_rename")->setEnabled(
false);
535 m_actionCollection->action(
"session_delete")->setEnabled(
false);
539 m_actionCollection->action(
"session_save")->setEnabled(
true);
540 m_actionCollection->action(
"session_rename")->setEnabled(
true);
541 m_actionCollection->action(
"session_delete")->setEnabled(
true);
546 readOnlyAction->setEnabled(
false);
547 readOnlyAction->setChecked(
false);
551 readOnlyAction->setEnabled(
true);
554 int sessId = sessionItem->getSessionId();
555 int activeSessId = m_sessionManager->getActiveSessionId();
556 m_actionCollection->action(
"session_save_as")->setEnabled(
true);
557 m_actionCollection->action(
"session_reload")->setEnabled(sessId == activeSessId);
558 m_actionCollection->action(
"session_activate")->setEnabled(sessId != activeSessId);
559 m_actionCollection->action(
"session_move_up")->setEnabled(sessId > 0);
560 m_actionCollection->action(
"session_move_down")->setEnabled(sessId < (m_sessionManager->getSessionCount() - 1));
563 emit selectionChanged();
567 void KateSessionPanel::slotSessionActivated(
int newSessionId,
int oldSessionId)
570 TQListViewItem *item = NULL;
571 if (oldSessionId != KateSessionManager::INVALID_SESSION)
575 item = m_listview->firstChild();
576 for (
int idx = 0; idx < oldSessionId; ++idx)
578 item = item->nextSibling();
580 item->setPixmap(m_columnPixmap, TQPixmap());
583 item = m_listview->firstChild();
584 for (
int idx = 0; idx < newSessionId; ++idx)
586 item = item->nextSibling();
588 item->setPixmap(m_columnPixmap, SmallIcon(
"ok"));
589 m_listview->setSelected(item,
true);
590 slotSelectionChanged();
594 void KateSessionPanel::slotSessionCreated(
int sessionId)
596 TQPtrList<KateSession>& sessions = m_sessionManager->getSessionsList();
597 new KateSessionPanelItem(m_listview, m_listview->lastItem(), sessions[sessionId]->getSessionName(),
602 void KateSessionPanel::slotSessionDeleted(
int sessionId)
605 TQListViewItem *item = m_listview->firstChild();
607 for (; idx < sessionId; ++idx)
609 item = item->nextSibling();
611 TQListViewItem *nextItem = item->nextSibling();
617 dynamic_cast<KateSessionPanelItem*
>(item)->setSessionId(idx++);
618 item = item->nextSibling();
623 void KateSessionPanel::slotSessionsSwapped(
int sessionIdMin,
int sessionIdMax)
625 if (sessionIdMin == sessionIdMax)
630 if (sessionIdMin > sessionIdMax)
634 int tmp = sessionIdMin;
635 sessionIdMin = sessionIdMax;
639 TQListViewItem *selectedItem = m_listview->selectedItem();
642 TQListViewItem *siblMin(NULL), *siblMax(NULL), *itemMin(NULL), *itemMax(NULL);
643 TQListViewItem *currItem = m_listview->firstChild();
644 TQListViewItem *nextItem(NULL);
647 nextItem = currItem->nextSibling();
648 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(nextItem);
649 if (sessionItem->getSessionId() == sessionIdMin)
654 else if (sessionItem->getSessionId() == sessionIdMax)
665 itemMin = m_listview->firstChild();
668 m_listview->takeItem(itemMax);
669 m_listview->takeItem(itemMin);
670 m_listview->insertItem(itemMin);
671 m_listview->insertItem(itemMax);
672 itemMax->moveItem(siblMin);
673 if (siblMax != itemMin)
675 itemMin->moveItem(siblMax);
679 itemMin->moveItem(itemMax);
682 (
dynamic_cast<KateSessionPanelItem*
>(itemMax))->setSessionId(sessionIdMin);
683 (
dynamic_cast<KateSessionPanelItem*
>(itemMin))->setSessionId(sessionIdMax);
685 m_listview->setSelected(selectedItem,
true);
689 void KateSessionPanel::slotSessionRenamed(
int sessionId)
691 TQListViewItem *item = m_listview->firstChild();
692 for (
int idx = 0; idx < sessionId; ++idx)
694 item = item->nextSibling();
696 item->setText(m_columnName, m_sessionManager->getSessionName(sessionId));
700 void KateSessionPanel::slotLVSessionRenamed(TQListViewItem *item)
702 KateSessionPanelItem *sessionItem =
dynamic_cast<KateSessionPanelItem*
>(item);
708 m_sessionManager->renameSession(sessionItem->getSessionId(), sessionItem->text(m_columnName));
712 int KateSessionPanel::handleSessionSwitch()
714 const KateSession *ks = m_sessionManager->getActiveSession();
715 int switchOption = m_sessionManager->getSwitchOption();
716 if (!ks || switchOption == KateSessionManager::SWITCH_DISCARD)
718 return KMessageBox::No;
721 if (switchOption == KateSessionManager::SWITCH_ASK)
723 KDialogBase *dlg =
new KDialogBase(i18n(
"Save Session"),
724 KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel,
725 KDialogBase::Cancel, KDialogBase::Cancel, NULL, NULL,
true,
false,
726 KStdGuiItem::save(), KStdGuiItem::del(), KStdGuiItem::cancel());
727 bool dontAgain =
false;
728 int res = KMessageBox::createKMessageBox(dlg, TQMessageBox::Warning,
729 i18n(
"<p>Do you want to save the current session?<p>!!NOTE!!"
730 "<p>The session will be removed if you choose \"Delete\""), TQStringList(),
731 i18n(
"Do not ask again"), &dontAgain, KMessageBox::Notify);
732 if (res == KDialogBase::Cancel)
734 return KMessageBox::Cancel;
738 if (res == KDialogBase::No)
740 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_DISCARD);
744 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_SAVE);
747 if (res == KDialogBase::No)
749 return KMessageBox::No;
757 KateSessionNameChooser *nameChooser =
new KateSessionNameChooser(
this,
false);
758 int res = nameChooser->exec();
759 if (res == TQDialog::Accepted)
761 m_sessionManager->renameSession(m_sessionManager->getActiveSessionId(), nameChooser->getSessionName());
765 return KMessageBox::Cancel;
769 return KMessageBox::Yes;
static KateApp * self()
static accessor to avoid casting ;)
The Kate session manager.
An object representing a Kate's session.
bool isStillVolatile() const
const TQString & getSessionFilename() const