MainWindow.cpp 10.1 KB
Newer Older
1
/*
2
    Copyright (C) 2006-2007 by Robert Knight <robertknight@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

20 21 22
// Own
#include "MainWindow.h"

23
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
24
#include <QtGui/QBoxLayout>
25

26 27 28 29
// KDE
#include <KAction>
#include <KActionCollection>
#include <KActionMenu>
30
#include <KShortcutsDialog>
31 32
#include <KLocale>
#include <KMenu>
33
#include <KMenuBar>
34
#include <KService>
35
#include <KToggleAction>
36
#include <KToggleFullScreenAction>
37
#include <KToolInvocation>
38
#include <kstandardaction.h>
39
#include <KXMLGUIFactory>
40 41

// Konsole
42
#include "BookmarkHandler.h"
43 44
#include "IncrementalSearchBar.h"
#include "RemoteConnectionDialog.h"
45
#include "SessionController.h"
46 47
#include "ProfileList.h"
#include "ManageProfilesDialog.h"
48 49
#include "ViewManager.h"
#include "ViewSplitter.h"
50

51 52
using namespace Konsole;

53
MainWindow::MainWindow()
54
 : KXmlGuiWindow() ,
55 56
   _bookmarkHandler(0),
   _pluggedController(0)
57
{
58
    // create actions for menus
59 60
    // the directory ('konsole') is included in the path here so that the XML
    // file can be found when this code is being used in the Konsole part.
61
    setXMLFile("konsole/konsoleui.rc");
62 63

    actionCollection()->setAssociatedWidget(this);
64 65 66
    setupActions();

    // create view manager
67
        _viewManager = new ViewManager(this,actionCollection());
Robert Knight's avatar
 
Robert Knight committed
68
    connect( _viewManager , SIGNAL(empty()) , this , SLOT(close()) );
69 70 71 72
    connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
            SLOT(activeViewChanged(SessionController*)) );
    connect( _viewManager , SIGNAL(viewPropertiesChanged(const QList<ViewProperties*>&)) ,
           bookmarkHandler() , SLOT(setViews(const QList<ViewProperties*>&)) ); 
73

74 75 76
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

77 78 79
    // create main window widgets
    setupWidgets();

80 81
    // create menus
    createGUI();
82 83 84 85 86 87 88 89 90 91 92 93 94 95

    // replace standard shortcuts which cannot be used in a terminal
    // (as they are reserved for use by terminal programs)
    correctShortcuts();
}

void MainWindow::correctShortcuts()
{
    // replace F1 shortcut for help contents
    QAction* helpAction = actionCollection()->action("help_contents");
    
    Q_ASSERT( helpAction );

    helpAction->setShortcut( QKeySequence() );
96 97
}

98 99 100 101 102 103 104 105 106
void MainWindow::setDefaultProfile(const QString& key)
{
    _defaultProfile = key;
}
QString MainWindow::defaultProfile() const
{
    return _defaultProfile;
}

107
ViewManager* MainWindow::viewManager() const
108 109 110 111
{
    return _viewManager;
}

112 113 114 115 116
void MainWindow::activeViewChanged(SessionController* controller)
{
    if ( _pluggedController == controller )
        return;

117
    // associate bookmark menu with current session
118 119 120 121 122 123 124
    bookmarkHandler()->setActiveView(controller);
    disconnect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , 0 , 0 );
    connect( bookmarkHandler() , SIGNAL(openUrl(const KUrl&)) , controller ,
             SLOT(openUrl(const KUrl&)) );

    // set the current session's search bar
    controller->setSearchBar( searchBar() );
125
    controller->setShowMenuAction( _toggleMenuBarAction );
126 127 128 129 130 131

    // listen for title changes from the current session
    if ( _pluggedController )
    {
        disconnect( _pluggedController , SIGNAL(titleChanged(ViewProperties*)) 
                     , this , SLOT(activeViewTitleChanged(ViewProperties*)) );
132
        guiFactory()->removeClient(_pluggedController);
133 134
    }

135 136
    Q_ASSERT( controller );

137 138 139 140 141 142 143 144 145 146 147 148 149 150
    connect( controller , SIGNAL(titleChanged(ViewProperties*)) , 
            this , SLOT(activeViewTitleChanged(ViewProperties*)) );

    guiFactory()->addClient(controller);

    // update session title to match newly activated session
    activeViewTitleChanged(controller);

    _pluggedController = controller;
}

void MainWindow::activeViewTitleChanged(ViewProperties* properties)
{
    setPlainCaption(properties->title());
151
    setWindowIcon(properties->icon());
152 153
}

154
IncrementalSearchBar* MainWindow::searchBar() const
155 156 157 158
{
    return _searchBar;
}

159
void MainWindow::setupActions()
160
{
161 162
    KActionCollection* collection = actionCollection();

163
    // File Menu
164 165 166
    QAction* newTabAction = collection->addAction("new-tab"); 
    newTabAction->setIcon( KIcon("openterm") );
    newTabAction->setText( i18n("New &Tab") );
Robert Knight's avatar
 
Robert Knight committed
167
    newTabAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_N) );
168
    connect( newTabAction , SIGNAL(triggered()) , this , SLOT(newTab()) );
169 170

    QAction* newWindowAction = collection->addAction("new-window"); 
Robert Knight's avatar
Robert Knight committed
171
    newWindowAction->setIcon( KIcon("window-new") );
172
    newWindowAction->setText( i18n("New &Window") );
Robert Knight's avatar
 
Robert Knight committed
173
    newWindowAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_M) );
174
    connect( newWindowAction , SIGNAL(triggered()) , this , SLOT(newWindow()) );
175

176 177 178 179 180 181
    QAction* remoteConnectionAction = collection->addAction("remote-connection");
    remoteConnectionAction->setText( i18n("Remote Connection...") );
    remoteConnectionAction->setIcon( KIcon("network") );
    remoteConnectionAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_R) );
    connect( remoteConnectionAction , SIGNAL(triggered()) , this , SLOT(showRemoteConnectionDialog()) ); 

182
       
183
#ifndef KONSOLE_PART 
184 185 186 187
    QAction* quitAction = KStandardAction::quit( this , SLOT(close()) , collection );
    // the default shortcut for quit is typically Ctrl+[Some Letter, usually Q] but that is reserved for 
    // use by terminal applications
    quitAction->setShortcut(Qt::CTRL+Qt::SHIFT+Qt::Key_Q);
188
#endif
189 190

    // Bookmark Menu
191
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
192
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
193
    collection->addAction("bookmark" , bookmarkMenu);
194

195 196 197
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

198
    // View Menu
199 200 201
    _toggleMenuBarAction = new KToggleAction(this);
    _toggleMenuBarAction->setText( i18n("Show Menu Bar") );
    _toggleMenuBarAction->setIcon( KIcon("show-menu") );
202
    _toggleMenuBarAction->setCheckedState( KGuiItem( i18n("Hide Menu Bar") , "show-menu") ); 
203 204 205
    _toggleMenuBarAction->setChecked( !menuBar()->isHidden() );
    connect( _toggleMenuBarAction , SIGNAL(toggled(bool)) , menuBar() , SLOT(setVisible(bool)) );
    collection->addAction("show-menubar",_toggleMenuBarAction);
206

207 208 209 210 211
    // Hide the Show/Hide menubar item if the menu bar is a MacOS-style menu bar
    if ( menuBar()->isTopLevelMenu() )
        _toggleMenuBarAction->setVisible(false);

    // Full Screen
212 213
    KToggleFullScreenAction* fullScreenAction = new KToggleFullScreenAction(this);
    fullScreenAction->setWindow(this);
214
    fullScreenAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_F11 );
215 216
    collection->addAction("view-full-screen",fullScreenAction);
    connect( fullScreenAction , SIGNAL(toggled(bool)) , this , SLOT(viewFullScreen(bool)) ); 
217
  
218
    // Settings Menu
219 220
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
221 222 223

    QAction* manageProfilesAction = collection->addAction("manage-profiles");
    manageProfilesAction->setText( i18n("Manage Profiles...") );
224
    manageProfilesAction->setIcon( KIcon("configure") );
225 226
    connect( manageProfilesAction , SIGNAL(triggered()) , this , SLOT(showManageProfilesDialog()) );

227 228
}

229 230 231 232 233 234 235
void MainWindow::viewFullScreen(bool fullScreen) 
{
    if ( fullScreen )
        showFullScreen();
    else
        showNormal();
}
236
BookmarkHandler* MainWindow::bookmarkHandler() const
237 238 239 240
{
    return _bookmarkHandler;
}

241
void MainWindow::setSessionList(ProfileList* list)
242
{
243
    sessionListChanged(list->actions());
244

245 246
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
247 248 249 250 251 252 253

    connect( list , SIGNAL(actionsChanged(const QList<QAction*>&)) , this ,
            SLOT(sessionListChanged(const QList<QAction*>&)) );
}

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
254 255
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
256 257
}

258 259 260 261 262 263 264 265
QString MainWindow::activeSessionDir() const
{
    if ( _pluggedController )
        return _pluggedController->currentDir();
    else
        return QString();
}

266
void MainWindow::newTab()
267
{
268
    emit newSessionRequest( _defaultProfile , activeSessionDir() , _viewManager);
269 270
}

271
void MainWindow::newWindow()
272
{
273
    emit newWindowRequest( _defaultProfile , activeSessionDir() );
274 275
}

276
void MainWindow::showShortcutsDialog()
277
{
278 279
    KShortcutsDialog::configure( actionCollection() ,
                                 KShortcutsEditor::LetterShortcutsDisallowed );
280 281
}

282
void MainWindow::newFromProfile(const QString& key)
283
{
284
        emit newSessionRequest(key,QString(),_viewManager);
285
}
286
void MainWindow::showManageProfilesDialog()
287
{
288 289
    ManageProfilesDialog* dialog = new ManageProfilesDialog(this);
    dialog->show();
290
}
291

292 293 294 295
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
296
        emit newSessionRequest(dialog.sessionKey(),QString(),_viewManager);
297 298
}

299
void MainWindow::setupWidgets()
300
{
301 302 303 304 305
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

    _searchBar = new IncrementalSearchBar( IncrementalSearchBar::AllFeatures , this);
    _searchBar->setVisible(false);
306

307 308 309 310
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
311

312 313 314
    widget->setLayout(layout);
    
    setCentralWidget(widget);
315
}
316

317
#include "MainWindow.moc"