katesessionpanel.cpp
00001 /* 00002 This file is part of the TDE project 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License version 2 as published by the Free Software Foundation. 00007 00008 This library is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 Library General Public License for more details. 00012 00013 You should have received a copy of the GNU Library General Public License 00014 along with this library; see the file COPYING.LIB. If not, write to 00015 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 Boston, MA 02110-1301, USA. 00017 */ 00018 00019 #include "katesessionpanel.h" 00020 #include "katesessionpanel.moc" 00021 00022 #include "kateapp.h" 00023 #include "katemainwindow.h" 00024 #include "kateviewmanager.h" 00025 #include "katesession.h" 00026 00027 #include <kiconloader.h> 00028 #include <tdelocale.h> 00029 #include <tdemessagebox.h> 00030 #include <tqlistview.h> 00031 #include <tqlabel.h> 00032 00033 00034 namespace 00035 { 00036 const char *KS_UNNAMED = "Unnamed"; 00037 }; 00038 00039 00040 //BEGIN KateSessionNameChooser 00041 //------------------------------------------- 00042 KateSessionNameChooser::KateSessionNameChooser(TQWidget *parent, bool showSwitchTo) 00043 : KDialogBase(parent, "", true, i18n("Session Name Chooser"), KDialogBase::User1 | KDialogBase::User2, 00044 KDialogBase::User2, true, KStdGuiItem::cancel(), KGuiItem(i18n("Continue"), "document-new")), 00045 m_showSwitchTo(showSwitchTo) 00046 { 00047 TQHBox *page = new TQHBox(this); 00048 //page->setMinimumSize(300, 100); 00049 setMainWidget(page); 00050 00051 TQVBox *vb = new TQVBox(page); 00052 vb->setSpacing(KDialog::spacingHint()); 00053 00054 TQLabel *label = new TQLabel(vb); 00055 label->setText("Please type the new session name:"); 00056 00057 m_sessionNameLE = new TQLineEdit(vb); 00058 m_sessionNameLE->setText(i18n(KS_UNNAMED)); 00059 m_sessionNameLE->setFocus(); 00060 00061 if (m_showSwitchTo) 00062 { 00063 m_activateCB = new TQCheckBox(i18n("Switch to the new session"), vb, NULL); 00064 m_activateCB->setChecked(true); 00065 } 00066 00067 connect(m_sessionNameLE, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(slotTextChanged())); 00068 slotTextChanged(); // update button status 00069 } 00070 00071 //------------------------------------------- 00072 TQString KateSessionNameChooser::getSessionName() 00073 { 00074 return m_sessionNameLE->text(); 00075 } 00076 00077 //------------------------------------------- 00078 bool KateSessionNameChooser::getActivateFlag() 00079 { 00080 if (m_showSwitchTo) 00081 { 00082 return m_activateCB->isChecked(); 00083 } 00084 return false; 00085 } 00086 00087 //------------------------------------------- 00088 void KateSessionNameChooser::slotUser1() 00089 { 00090 reject(); 00091 } 00092 00093 //------------------------------------------- 00094 void KateSessionNameChooser::slotUser2() 00095 { 00096 accept(); 00097 } 00098 00099 //------------------------------------------- 00100 void KateSessionNameChooser::slotTextChanged() 00101 { 00102 enableButton(KDialogBase::User2, !m_sessionNameLE->text().isEmpty()); 00103 } 00104 //END KateSessionNameChooser 00105 00106 00107 //BEGIN KateSessionPanelToolBarParent 00108 void KateSessionPanelToolBarParent::setToolBar(TDEToolBar *tbar) 00109 { 00110 m_tbar = tbar; 00111 } 00112 00113 //------------------------------------------- 00114 void KateSessionPanelToolBarParent::resizeEvent (TQResizeEvent*) 00115 { 00116 if (m_tbar) 00117 { 00118 setMinimumHeight(m_tbar->sizeHint().height()); 00119 m_tbar->resize(width(),height()); 00120 } 00121 } 00122 //END KateSessionPanelToolBarParent 00123 00124 00125 //BEGIN KateSessionPanel 00126 //------------------------------------------- 00127 KateSessionPanel::KateSessionPanel(KateMainWindow *mainWindow, KateViewManager *viewManager, 00128 TQWidget *parent, const char *name) 00129 : TQVBox(parent, name), m_sessionManager(KateSessionManager::self()), 00130 m_actionCollection(new TDEActionCollection(this)), m_columnName(-1), m_columnPixmap(-1) 00131 { 00132 // Toolbar 00133 setup_toolbar(); 00134 00135 // Listview 00136 m_listview = new TDEListView(this); 00137 m_listview->header()->hide(); 00138 m_columnName = m_listview->addColumn("Session name"); 00139 m_columnPixmap = m_listview->addColumn("Pixmap", 24); 00140 m_listview->addColumn("Dummy", 1); // Dummy column, only for nice resizing 00141 m_listview->header()->setResizeEnabled(false, m_columnPixmap); 00142 m_listview->setColumnAlignment(m_columnPixmap, TQt::AlignCenter); 00143 m_listview->setMinimumWidth(m_listview->sizeHint().width()); 00144 m_listview->setSorting(-1); 00145 m_listview->setResizeMode(TQListView::LastColumn); 00146 //m_listview->setRootIsDecorated(true); // FIXME disabled until doc list software is developed 00147 00148 00149 connect(m_listview, TQT_SIGNAL(selectionChanged()), 00150 this, TQT_SLOT(slotSelectionChanged())); 00151 connect(m_listview, TQT_SIGNAL(executed(TQListViewItem*)), 00152 this, TQT_SLOT(slotItemExecuted(TQListViewItem*))); 00153 connect(m_listview, TQT_SIGNAL(returnPressed(TQListViewItem*)), 00154 this, TQT_SLOT(slotItemExecuted(TQListViewItem*))); 00155 connect(KateApp::self(), TQT_SIGNAL(optionsChanged()), 00156 this, TQT_SLOT(slotSelectionChanged())); 00157 connect(m_sessionManager, TQT_SIGNAL(switchOptionChanged()), 00158 this, TQT_SLOT(slotSelectionChanged())); 00159 connect(m_sessionManager, TQT_SIGNAL(sessionActivated(int, int)), 00160 this, TQT_SLOT(slotSessionActivated(int, int))); 00161 connect(m_sessionManager, TQT_SIGNAL(sessionCreated(int)), 00162 this, TQT_SLOT(slotSessionCreated(int))); 00163 connect(m_sessionManager, TQT_SIGNAL(sessionDeleted(int)), 00164 this, TQT_SLOT(slotSessionDeleted(int))); 00165 connect(m_sessionManager, TQT_SIGNAL(sessionsSwapped(int, int)), 00166 this, TQT_SLOT(slotSessionsSwapped(int, int))); 00167 connect(m_sessionManager, TQT_SIGNAL(sessionRenamed(int)), 00168 this, TQT_SLOT(slotSessionRenamed(int))); 00169 connect(m_listview, TQT_SIGNAL(itemRenamed(TQListViewItem*)), 00170 this, TQT_SLOT(slotLVSessionRenamed(TQListViewItem*))); 00171 00172 TQPtrList<KateSession>& sessions = m_sessionManager->getSessionsList(); 00173 for (int idx = sessions.count() - 1; idx >= 0; --idx) 00174 { 00175 new KateSessionPanelItem(m_listview, sessions[idx]->getSessionName(), idx); 00176 if (idx == m_sessionManager->getActiveSessionId()) 00177 { 00178 m_listview->setSelected(m_listview->firstChild(), true); 00179 m_listview->firstChild()->setPixmap(m_columnPixmap, SmallIcon("ok")); 00180 } 00181 } 00182 00183 } 00184 00185 //------------------------------------------- 00186 void KateSessionPanel::setup_toolbar() 00187 { 00188 // Toolbar widget and frame 00189 KateSessionPanelToolBarParent *tbarParent = new KateSessionPanelToolBarParent(this); 00190 m_toolbar = new TDEToolBar(tbarParent, "Kate Session Panel Toolbar", true); 00191 tbarParent->setToolBar(m_toolbar); 00192 m_toolbar->setMovingEnabled(false); 00193 m_toolbar->setFlat(true); 00194 m_toolbar->setIconText(TDEToolBar::IconOnly); 00195 m_toolbar->setIconSize(16); 00196 m_toolbar->setEnableContextMenu(false); 00197 00198 // Toolbar actions 00199 TDEAction *a; 00200 00201 a = new TDEAction(i18n("New"), SmallIcon("list-add"), 0, 00202 TQT_TQOBJECT(this), TQT_SLOT(slotNewSession()), m_actionCollection, "session_new"); 00203 a->setWhatsThis(i18n("Create a new session and switch to it.")); 00204 a->plug(m_toolbar); 00205 00206 a = new TDEAction(i18n("Save"), SmallIcon("document-save"), 0, 00207 TQT_TQOBJECT(this), TQT_SLOT(slotSaveSession()), m_actionCollection, "session_save"); 00208 a->setWhatsThis(i18n("Save the selected session.")); 00209 a->plug(m_toolbar); 00210 00211 a = new TDEAction(i18n("Save as..."), SmallIcon("document-save-as"), 0, 00212 TQT_TQOBJECT(this), TQT_SLOT(slotSaveSessionAs()), m_actionCollection, "session_save_as"); 00213 a->setWhatsThis(i18n("Save an unsaved session with a new name or clone an already saved session " 00214 "into a new session.")); 00215 a->plug(m_toolbar); 00216 00217 a = new TDEAction(i18n("Rename"), SmallIcon("edit_user"), 0, 00218 TQT_TQOBJECT(this), TQT_SLOT(slotRenameSession()), m_actionCollection, "session_rename"); 00219 a->setWhatsThis(i18n("Rename the selected session.")); 00220 a->plug(m_toolbar); 00221 00222 a = new TDEAction(i18n("Delete"), SmallIcon("edit-delete"), 0, 00223 TQT_TQOBJECT(this), TQT_SLOT(slotDeleteSession()), m_actionCollection, "session_delete"); 00224 a->setWhatsThis(i18n("Delete the selected session.")); 00225 a->plug(m_toolbar); 00226 00227 a = new TDEAction(i18n("Reload"), SmallIcon("reload"), 0, 00228 TQT_TQOBJECT(this), TQT_SLOT(slotReloadSession()), m_actionCollection, "session_reload"); 00229 a->setWhatsThis(i18n("Reload the last saved state of the selected session.")); 00230 a->plug(m_toolbar); 00231 00232 m_toolbar->insertLineSeparator(); 00233 00234 a = new TDEAction(i18n("Activate"), SmallIcon("forward"), 0, 00235 TQT_TQOBJECT(this), TQT_SLOT(slotActivateSession()), m_actionCollection, "session_activate"); 00236 a->setWhatsThis(i18n("Activate the selected session.")); 00237 a->plug(m_toolbar); 00238 00239 TDEToggleAction *tglA = new TDEToggleAction(i18n("Toggle read only"), SmallIcon("encrypted"), 0, 00240 TQT_TQOBJECT(this), TQT_SLOT(slotSessionToggleReadOnly()), m_actionCollection, "session_toggle_read_only"); 00241 tglA->setWhatsThis(i18n("Toggle read only status for the selected session.<p>" 00242 "In a read only session, you can work as usual but the list of documents in the session " 00243 "will not be saved when you exit Kate or switch to another session.<p>" 00244 "You can use this option to create template sessions that you wish to keep unchanged over time.")); 00245 tglA->plug(m_toolbar); 00246 00247 a = new TDEAction(i18n("Move Up"), SmallIcon("go-up"), 0, 00248 TQT_TQOBJECT(this), TQT_SLOT(slotSessionMoveUp()), m_actionCollection, "session_move_up"); 00249 a->setWhatsThis(i18n("Move up the selected session.")); 00250 a->plug(m_toolbar); 00251 00252 a = new TDEAction(i18n("Move Down"), SmallIcon("go-down"), 0, 00253 TQT_TQOBJECT(this), TQT_SLOT(slotSessionMoveDown()), m_actionCollection, "session_move_down"); 00254 a->setWhatsThis(i18n("Move down the selected session.")); 00255 a->plug(m_toolbar); 00256 } 00257 00258 //------------------------------------------- 00259 void KateSessionPanel::slotNewSession() 00260 { 00261 KateSessionNameChooser *nameChooser = new KateSessionNameChooser(this, false); 00262 int result = nameChooser->exec(); 00263 if (result == TQDialog::Accepted) 00264 { 00265 int res = handleSessionSwitch(); 00266 if (res == KMessageBox::Cancel) 00267 { 00268 return; 00269 } 00270 else 00271 { 00272 m_sessionManager->newSession(nameChooser->getSessionName(), res == KMessageBox::Yes); 00273 } 00274 } 00275 } 00276 00277 //------------------------------------------- 00278 void KateSessionPanel::slotSaveSession() 00279 { 00280 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00281 if (!sessionItem) 00282 { 00283 return; 00284 } 00285 00286 int sessId = sessionItem->getSessionId(); 00287 const KateSession *ks = m_sessionManager->getSessionFromId(sessId); 00288 if (!ks) 00289 { 00290 return; 00291 } 00292 00293 if (ks->isStillVolatile()) 00294 { 00295 // Session has never been saved before. Ask user for a session name first 00296 slotSaveSessionAs(); 00297 } 00298 else 00299 { 00300 m_sessionManager->saveSession(sessId); 00301 slotSelectionChanged(); // Update the toolbar button status 00302 } 00303 } 00304 00305 //------------------------------------------- 00306 void KateSessionPanel::slotSaveSessionAs() 00307 { 00308 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00309 if (!sessionItem) 00310 { 00311 return; 00312 } 00313 int sessId = sessionItem->getSessionId(); 00314 KateSession *ks = m_sessionManager->getSessionFromId(sessId); 00315 if (!ks) 00316 { 00317 return; 00318 } 00319 00320 // If the session was never saved or named before, the session will be saved with a new name. 00321 // Otherwise it will be cloned into a new session. 00322 bool cloneSession = !ks->isStillVolatile(); 00323 // Get new session name 00324 KateSessionNameChooser *nameChooser = new KateSessionNameChooser(this, cloneSession); 00325 int result = nameChooser->exec(); 00326 if (result == TQDialog::Accepted) 00327 { 00328 if (!cloneSession) 00329 { 00330 // Save unsaved session 00331 m_sessionManager->renameSession(sessId, nameChooser->getSessionName()); 00332 m_sessionManager->saveSession(sessId); 00333 } 00334 else 00335 { 00336 // Clone session 00337 bool activate = nameChooser->getActivateFlag(); 00338 int activeSessionId = m_sessionManager->getActiveSessionId(); 00339 int res = KMessageBox::Yes; 00340 if (activate && sessId != activeSessionId) 00341 { 00342 // Cloning another session and switching to it at the same time, 00343 // handle session switch correctly 00344 res = handleSessionSwitch(); 00345 if (res == KMessageBox::Cancel) 00346 { 00347 return; 00348 } 00349 } 00350 m_sessionManager->cloneSession(sessId, nameChooser->getSessionName(), activate, res == KMessageBox::No); 00351 } 00352 } 00353 00354 slotSelectionChanged(); // Update the toolbar button status 00355 } 00356 00357 //------------------------------------------- 00358 void KateSessionPanel::slotRenameSession() 00359 { 00360 TQListViewItem *sessionItem = m_listview->selectedItem(); 00361 if (!sessionItem) 00362 { 00363 return; 00364 } 00365 00366 m_listview->rename(m_listview->selectedItem(), m_columnName); 00367 } 00368 00369 //------------------------------------------- 00370 void KateSessionPanel::slotDeleteSession() 00371 { 00372 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00373 if (!sessionItem) 00374 { 00375 return; 00376 } 00377 00378 int result = KMessageBox::warningContinueCancel(this, 00379 i18n("Do you really want to delete the session \"%1\"?").arg(sessionItem->text(0)), 00380 i18n("Delete session"), KStdGuiItem::del()); 00381 if (result == KMessageBox::Continue) 00382 { 00383 int sessionId = sessionItem->getSessionId(); 00384 if (sessionId == m_sessionManager->getActiveSessionId()) 00385 { 00386 // First check if all documents can be closed safely 00387 if (KateApp::self()->activeMainWindow()) 00388 { 00389 if (!KateApp::self()->activeMainWindow()->queryClose_internal()) 00390 return; 00391 } 00392 } 00393 //FIXME add options to let user decide what to do when deleting the current session 00394 //(open previous/next session, create new empty session) 00395 m_sessionManager->deleteSession(sessionId, KateSessionManager::INVALID_SESSION); 00396 } 00397 } 00398 00399 //------------------------------------------- 00400 void KateSessionPanel::slotReloadSession() 00401 { 00402 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00403 if (!sessionItem) 00404 { 00405 return; 00406 } 00407 int sessId = sessionItem->getSessionId(); 00408 if (sessId != m_sessionManager->getActiveSessionId()) 00409 { 00410 return; 00411 } 00412 00413 // Restore active session to the last saved state 00414 m_sessionManager->reloadActiveSession(); 00415 } 00416 00417 //------------------------------------------- 00418 void KateSessionPanel::slotActivateSession() 00419 { 00420 KateSessionPanelItem *newSessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00421 if (!newSessionItem) 00422 { 00423 return; 00424 } 00425 00426 int currSessionId = m_sessionManager->getActiveSessionId(); 00427 int newSessionId = newSessionItem->getSessionId(); 00428 if (newSessionId != currSessionId) 00429 { 00430 int res = handleSessionSwitch(); 00431 if (res == KMessageBox::Cancel) 00432 { 00433 return; 00434 } 00435 else 00436 { 00437 m_sessionManager->activateSession(newSessionId, res == KMessageBox::Yes); 00438 } 00439 } 00440 } 00441 00442 //------------------------------------------- 00443 void KateSessionPanel::slotSessionToggleReadOnly() 00444 { 00445 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00446 const KateSession *ks(NULL); 00447 if (sessionItem) 00448 { 00449 ks = m_sessionManager->getSessionFromId(sessionItem->getSessionId()); 00450 } 00451 if (!sessionItem || !ks) 00452 { 00453 return; 00454 } 00455 00456 m_sessionManager->setSessionReadOnlyStatus(sessionItem->getSessionId(), !ks->isReadOnly()); 00457 slotSelectionChanged(); // Update the toolbar button status 00458 } 00459 00460 //------------------------------------------- 00461 void KateSessionPanel::slotSessionMoveUp() 00462 { 00463 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00464 if (!sessionItem) 00465 { 00466 return; 00467 } 00468 00469 m_sessionManager->moveSessionBackward(sessionItem->getSessionId()); 00470 } 00471 00472 //------------------------------------------- 00473 void KateSessionPanel::slotSessionMoveDown() 00474 { 00475 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00476 if (!sessionItem) 00477 { 00478 return; 00479 } 00480 00481 m_sessionManager->moveSessionForward(sessionItem->getSessionId()); 00482 } 00483 00484 //------------------------------------------- 00485 void KateSessionPanel::slotItemExecuted(TQListViewItem *item) 00486 { 00487 if (!item) 00488 { 00489 return; 00490 } 00491 00492 // First level items are sessions. Executing one, will switch to that session. 00493 // This is only allow when the 'Activate' toolbar button is enabled 00494 if (!item->parent() && 00495 m_actionCollection->action("session_activate")->isEnabled()) 00496 { 00497 slotActivateSession(); 00498 return; 00499 } 00500 } 00501 00502 //------------------------------------------- 00503 void KateSessionPanel::slotSelectionChanged() 00504 { 00505 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(m_listview->selectedItem()); 00506 const KateSession *ks(NULL); 00507 if (sessionItem) 00508 { 00509 ks = m_sessionManager->getSessionFromId(sessionItem->getSessionId()); 00510 } 00511 00512 TDEToggleAction *readOnlyAction = dynamic_cast<TDEToggleAction*>( 00513 m_actionCollection->action("session_toggle_read_only")); 00514 if (!sessionItem || !ks || 00515 m_sessionManager->getSwitchOption() == KateSessionManager::SWITCH_DISCARD) 00516 { 00517 m_actionCollection->action("session_save")->setEnabled(false); 00518 m_actionCollection->action("session_save_as")->setEnabled(false); 00519 m_actionCollection->action("session_rename")->setEnabled(false); 00520 m_actionCollection->action("session_delete")->setEnabled(false); 00521 m_actionCollection->action("session_reload")->setEnabled(false); 00522 m_actionCollection->action("session_activate")->setEnabled(false); 00523 m_actionCollection->action("session_move_up")->setEnabled(false); 00524 m_actionCollection->action("session_move_down")->setEnabled(false); 00525 readOnlyAction->setEnabled(false); 00526 readOnlyAction->setChecked(false); 00527 } 00528 else 00529 { 00530 if (ks->isReadOnly()) 00531 { 00532 // Read only sessions can not be saved or renamed 00533 m_actionCollection->action("session_save")->setEnabled(false); 00534 m_actionCollection->action("session_rename")->setEnabled(false); 00535 m_actionCollection->action("session_delete")->setEnabled(false); 00536 } 00537 else 00538 { 00539 m_actionCollection->action("session_save")->setEnabled(true); 00540 m_actionCollection->action("session_rename")->setEnabled(true); 00541 m_actionCollection->action("session_delete")->setEnabled(true); 00542 } 00543 if (ks->getSessionFilename().isEmpty()) 00544 { 00545 // Unstored sessions can not be made readonly 00546 readOnlyAction->setEnabled(false); 00547 readOnlyAction->setChecked(false); 00548 } 00549 else 00550 { 00551 readOnlyAction->setEnabled(true); 00552 readOnlyAction->setChecked(ks->isReadOnly()); 00553 } 00554 int sessId = sessionItem->getSessionId(); 00555 int activeSessId = m_sessionManager->getActiveSessionId(); 00556 m_actionCollection->action("session_save_as")->setEnabled(true); 00557 m_actionCollection->action("session_reload")->setEnabled(sessId == activeSessId); 00558 m_actionCollection->action("session_activate")->setEnabled(sessId != activeSessId); 00559 m_actionCollection->action("session_move_up")->setEnabled(sessId > 0); 00560 m_actionCollection->action("session_move_down")->setEnabled(sessId < (m_sessionManager->getSessionCount() - 1)); 00561 } 00562 00563 emit selectionChanged(); 00564 } 00565 00566 //------------------------------------------- 00567 void KateSessionPanel::slotSessionActivated(int newSessionId, int oldSessionId) 00568 { 00569 // Move the active session marker 00570 TQListViewItem *item = NULL; 00571 if (oldSessionId != KateSessionManager::INVALID_SESSION) 00572 { 00573 // Old volatile sessions may have already been deleted. 00574 // Remove the marker only for valid sessions. 00575 item = m_listview->firstChild(); 00576 for (int idx = 0; idx < oldSessionId; ++idx) 00577 { 00578 item = item->nextSibling(); 00579 } 00580 item->setPixmap(m_columnPixmap, TQPixmap()); 00581 } 00582 00583 item = m_listview->firstChild(); 00584 for (int idx = 0; idx < newSessionId; ++idx) 00585 { 00586 item = item->nextSibling(); 00587 } 00588 item->setPixmap(m_columnPixmap, SmallIcon("ok")); 00589 m_listview->setSelected(item, true); 00590 slotSelectionChanged(); // Update the toolbar button status 00591 } 00592 00593 //------------------------------------------- 00594 void KateSessionPanel::slotSessionCreated(int sessionId) 00595 { 00596 TQPtrList<KateSession>& sessions = m_sessionManager->getSessionsList(); 00597 new KateSessionPanelItem(m_listview, m_listview->lastItem(), sessions[sessionId]->getSessionName(), 00598 sessionId); 00599 } 00600 00601 //------------------------------------------- 00602 void KateSessionPanel::slotSessionDeleted(int sessionId) 00603 { 00604 // delete item from listview 00605 TQListViewItem *item = m_listview->firstChild(); 00606 int idx = 0; 00607 for (; idx < sessionId; ++idx) 00608 { 00609 item = item->nextSibling(); 00610 } 00611 TQListViewItem *nextItem = item->nextSibling(); 00612 delete item; 00613 // update session id of all following items 00614 item = nextItem; 00615 while (item) 00616 { 00617 dynamic_cast<KateSessionPanelItem*>(item)->setSessionId(idx++); 00618 item = item->nextSibling(); 00619 } 00620 } 00621 00622 //------------------------------------------- 00623 void KateSessionPanel::slotSessionsSwapped(int sessionIdMin, int sessionIdMax) 00624 { 00625 if (sessionIdMin == sessionIdMax) 00626 { 00627 return; 00628 } 00629 00630 if (sessionIdMin > sessionIdMax) 00631 { 00632 // this is not executed when the slot is connected to m_sessionManager's 00633 // sessionsSwapped(int, int) signal 00634 int tmp = sessionIdMin; 00635 sessionIdMin = sessionIdMax; 00636 sessionIdMax = tmp; 00637 } 00638 00639 TQListViewItem *selectedItem = m_listview->selectedItem(); 00640 00641 // Looks for the previous siblings of the two items 00642 TQListViewItem *siblMin(NULL), *siblMax(NULL), *itemMin(NULL), *itemMax(NULL); 00643 TQListViewItem *currItem = m_listview->firstChild(); 00644 TQListViewItem *nextItem(NULL); 00645 while (currItem) 00646 { 00647 nextItem = currItem->nextSibling(); 00648 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(nextItem); 00649 if (sessionItem->getSessionId() == sessionIdMin) 00650 { 00651 siblMin = currItem; 00652 itemMin = nextItem; 00653 } 00654 else if (sessionItem->getSessionId() == sessionIdMax) 00655 { 00656 siblMax = currItem; 00657 itemMax = nextItem; 00658 break; 00659 } 00660 currItem = nextItem; 00661 } 00662 if (!itemMin) 00663 { 00664 // The sessionIdMin item was the first of the list 00665 itemMin = m_listview->firstChild(); 00666 } 00667 // Remove the two items and place them in their new positions 00668 m_listview->takeItem(itemMax); 00669 m_listview->takeItem(itemMin); 00670 m_listview->insertItem(itemMin); 00671 m_listview->insertItem(itemMax); 00672 itemMax->moveItem(siblMin); 00673 if (siblMax != itemMin) 00674 { 00675 itemMin->moveItem(siblMax); 00676 } 00677 else 00678 { 00679 itemMin->moveItem(itemMax); 00680 } 00681 // Update item's session id 00682 (dynamic_cast<KateSessionPanelItem*>(itemMax))->setSessionId(sessionIdMin); 00683 (dynamic_cast<KateSessionPanelItem*>(itemMin))->setSessionId(sessionIdMax); 00684 00685 m_listview->setSelected(selectedItem, true); 00686 } 00687 00688 //------------------------------------------- 00689 void KateSessionPanel::slotSessionRenamed(int sessionId) 00690 { 00691 TQListViewItem *item = m_listview->firstChild(); 00692 for (int idx = 0; idx < sessionId; ++idx) 00693 { 00694 item = item->nextSibling(); 00695 } 00696 item->setText(m_columnName, m_sessionManager->getSessionName(sessionId)); 00697 } 00698 00699 //------------------------------------------- 00700 void KateSessionPanel::slotLVSessionRenamed(TQListViewItem *item) 00701 { 00702 KateSessionPanelItem *sessionItem = dynamic_cast<KateSessionPanelItem*>(item); 00703 if (!sessionItem) 00704 { 00705 return; 00706 } 00707 00708 m_sessionManager->renameSession(sessionItem->getSessionId(), sessionItem->text(m_columnName)); 00709 } 00710 00711 //------------------------------------------- 00712 int KateSessionPanel::handleSessionSwitch() 00713 { 00714 const KateSession *ks = m_sessionManager->getActiveSession(); 00715 int switchOption = m_sessionManager->getSwitchOption(); 00716 if (!ks || switchOption == KateSessionManager::SWITCH_DISCARD) 00717 { 00718 return KMessageBox::No; 00719 } 00720 00721 if (switchOption == KateSessionManager::SWITCH_ASK) 00722 { 00723 KDialogBase *dlg = new KDialogBase(i18n("Save Session"), 00724 KDialogBase::Yes | KDialogBase::No | KDialogBase::Cancel, 00725 KDialogBase::Cancel, KDialogBase::Cancel, NULL, NULL, true, false, 00726 KStdGuiItem::save(), KStdGuiItem::del(), KStdGuiItem::cancel()); 00727 bool dontAgain = false; 00728 int res = KMessageBox::createKMessageBox(dlg, TQMessageBox::Warning, 00729 i18n("<p>Do you want to save the current session?<p>!!NOTE!!" 00730 "<p>The session will be removed if you choose \"Delete\""), TQStringList(), 00731 i18n("Do not ask again"), &dontAgain, KMessageBox::Notify); 00732 if (res == KDialogBase::Cancel) 00733 { 00734 return KMessageBox::Cancel; 00735 } 00736 if (dontAgain) 00737 { 00738 if (res == KDialogBase::No) 00739 { 00740 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_DISCARD); 00741 } 00742 else 00743 { 00744 m_sessionManager->setSwitchOption(KateSessionManager::SWITCH_SAVE); 00745 } 00746 } 00747 if (res == KDialogBase::No) 00748 { 00749 return KMessageBox::No; 00750 } 00751 } 00752 00753 // At this point the session needs to be saved. 00754 // Make sure to handle volatile sessions correctly. 00755 if (ks->isStillVolatile()) 00756 { 00757 KateSessionNameChooser *nameChooser = new KateSessionNameChooser(this, false); 00758 int res = nameChooser->exec(); 00759 if (res == TQDialog::Accepted) 00760 { 00761 m_sessionManager->renameSession(m_sessionManager->getActiveSessionId(), nameChooser->getSessionName()); 00762 } 00763 else 00764 { 00765 return KMessageBox::Cancel; 00766 } 00767 } 00768 00769 return KMessageBox::Yes; 00770 } 00771 //END KateSessionPanel
Trinity API Reference