MainWindow.cpp 9.45 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 64
    setupActions();

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

72 73 74
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

75 76 77
    // create main window widgets
    setupWidgets();

78 79 80 81
    // create menus
    createGUI();
}

82 83 84 85 86 87 88 89 90
void MainWindow::setDefaultProfile(const QString& key)
{
    _defaultProfile = key;
}
QString MainWindow::defaultProfile() const
{
    return _defaultProfile;
}

91
ViewManager* MainWindow::viewManager() const
92 93 94 95
{
    return _viewManager;
}

96 97 98 99 100 101 102 103 104 105 106 107 108
void MainWindow::activeViewChanged(SessionController* controller)
{
    if ( _pluggedController == controller )
        return;

    // associated bookmark menu with current session
    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() );
109
    controller->setShowMenuAction( _toggleMenuBarAction );
110 111 112 113 114 115

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

119 120
    Q_ASSERT( controller );

121 122 123 124 125 126 127 128 129 130 131 132 133 134
    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());
135
    setWindowIcon(properties->icon());
136 137
}

138
IncrementalSearchBar* MainWindow::searchBar() const
139 140 141 142
{
    return _searchBar;
}

143
void MainWindow::setupActions()
144
{
145 146
    KActionCollection* collection = actionCollection();

147
    // File Menu
148 149 150
    QAction* newTabAction = collection->addAction("new-tab"); 
    newTabAction->setIcon( KIcon("openterm") );
    newTabAction->setText( i18n("New &Tab") );
Robert Knight's avatar
 
Robert Knight committed
151
    newTabAction->setShortcut( QKeySequence(Qt::CTRL+Qt::SHIFT+Qt::Key_N) );
152
    connect( newTabAction , SIGNAL(triggered()) , this , SLOT(newTab()) );
153 154

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

160 161 162 163 164 165
    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()) ); 

166
       
167
#ifndef KONSOLE_PART 
168 169 170 171
    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);
172
#endif
173 174

    // Bookmark Menu
175
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
176
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
177
    collection->addAction("bookmark" , bookmarkMenu);
178

179 180 181
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

182
    // View Menu
183 184 185 186 187 188 189
    _toggleMenuBarAction = new KToggleAction(this);
    _toggleMenuBarAction->setText( i18n("Show Menu Bar") );
    _toggleMenuBarAction->setIcon( KIcon("show-menu") );
    _toggleMenuBarAction->setCheckedState( KGuiItem("Hide Menu Bar","show-menu") ); 
    _toggleMenuBarAction->setChecked( !menuBar()->isHidden() );
    connect( _toggleMenuBarAction , SIGNAL(toggled(bool)) , menuBar() , SLOT(setVisible(bool)) );
    collection->addAction("show-menubar",_toggleMenuBarAction);
190

191 192
    KToggleFullScreenAction* fullScreenAction = new KToggleFullScreenAction(this);
    fullScreenAction->setWindow(this);
193
    fullScreenAction->setShortcut( Qt::CTRL + Qt::SHIFT + Qt::Key_F11 );
194 195
    collection->addAction("view-full-screen",fullScreenAction);
    connect( fullScreenAction , SIGNAL(toggled(bool)) , this , SLOT(viewFullScreen(bool)) ); 
196 197 198 199 200
    //TODO - Implmement this correctly
    //
    //QAction* mergeAction = collection->addAction("merge-windows");
    //mergeAction->setText( i18n("&Merge Windows") );
    //connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );
201

202
    // Settings Menu
203 204
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
205 206 207

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

211 212
}

213 214 215 216 217 218 219
void MainWindow::viewFullScreen(bool fullScreen) 
{
    if ( fullScreen )
        showFullScreen();
    else
        showNormal();
}
220
BookmarkHandler* MainWindow::bookmarkHandler() const
221 222 223 224
{
    return _bookmarkHandler;
}

225
void MainWindow::setSessionList(ProfileList* list)
226
{
227
    sessionListChanged(list->actions());
228

229 230
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
231 232 233 234 235 236 237

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
238 239
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
240 241
}

242
void MainWindow::newTab()
243
{
244
    emit newSessionRequest( _defaultProfile , _viewManager);
245 246
}

247
void MainWindow::newWindow()
248
{
249
    emit newWindowRequest( _defaultProfile );
250 251
}

252
void MainWindow::showShortcutsDialog()
253
{
254 255
    KShortcutsDialog::configure( actionCollection() ,
                                 KShortcutsEditor::LetterShortcutsDisallowed );
256 257
}

258
void MainWindow::newFromProfile(const QString& key)
259
{
260
        emit newSessionRequest(key,_viewManager);
261
}
262
void MainWindow::showManageProfilesDialog()
263
{
264 265
    ManageProfilesDialog* dialog = new ManageProfilesDialog(this);
    dialog->show();
266
}
267

268 269 270 271
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
272
        emit newSessionRequest(dialog.sessionKey(),_viewManager);
273 274
}

275
void MainWindow::setupWidgets()
276
{
277 278 279 280 281
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

283 284 285 286
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
287

288 289 290
    widget->setLayout(layout);
    
    setCentralWidget(widget);
291
}
292

293
#include "MainWindow.moc"