MainWindow.cpp 9.06 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
// Qt
21
#include <QApplication>
22 23
#include <QVBoxLayout>

24 25 26 27
// KDE
#include <KAction>
#include <KActionCollection>
#include <KActionMenu>
28
#include <KShortcutsDialog>
29 30
#include <KLocale>
#include <KMenu>
31
#include <KMenuBar>
32 33
#include <KService>
#include <KToolInvocation>
34
#include <kstandardaction.h>
35
#include <KXMLGUIFactory>
36 37

// Konsole
38 39 40 41
#ifndef KONSOLE_PART
    #include "Application.h"
#endif

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

52 53
using namespace Konsole;

54
MainWindow::MainWindow()
55
 : KXmlGuiWindow() ,
56 57
   _bookmarkHandler(0),
   _pluggedController(0)
58
{
59 60 61
    // add a small amount of space between the top of the window and the main widget
    // to prevent the menu bar and main widget borders touching (which looks very ugly) in styles
    // where the menu bar has a lower border
62
    //setContentsMargins(0,2,0,0);
63
   
64
    // create actions for menus
65
    setXMLFile("konsole/konsoleui.rc");
66 67 68
    setupActions();

    // create view manager
69 70
    // 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.
71
    //setXMLFile("konsole/konsoleui.rc");
72
    _viewManager = new ViewManager(this,actionCollection());
Robert Knight's avatar
 
Robert Knight committed
73
    connect( _viewManager , SIGNAL(empty()) , this , SLOT(close()) );
74 75 76 77
    connect( _viewManager , SIGNAL(activeViewChanged(SessionController*)) , this ,
            SLOT(activeViewChanged(SessionController*)) );
    connect( _viewManager , SIGNAL(viewPropertiesChanged(const QList<ViewProperties*>&)) ,
           bookmarkHandler() , SLOT(setViews(const QList<ViewProperties*>&)) ); 
78

79 80 81
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

82 83 84
    // create main window widgets
    setupWidgets();

85 86 87 88
    // create menus
    createGUI();
}

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

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
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() );

    // listen for title changes from the current session
    if ( _pluggedController )
    {
        disconnect( _pluggedController , SIGNAL(titleChanged(ViewProperties*)) 
                     , this , SLOT(activeViewTitleChanged(ViewProperties*)) );
    }

    connect( controller , SIGNAL(titleChanged(ViewProperties*)) , 
            this , SLOT(activeViewTitleChanged(ViewProperties*)) );

    guiFactory()->removeClient(_pluggedController);
    guiFactory()->addClient(controller);

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

    _pluggedController = controller;
}

void MainWindow::activeViewTitleChanged(ViewProperties* properties)
{
    setPlainCaption(properties->title());
}

132
IncrementalSearchBar* MainWindow::searchBar() const
133 134 135 136
{
    return _searchBar;
}

137
void MainWindow::setupActions()
138
{
139 140
    KActionCollection* collection = actionCollection();

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

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

154 155 156 157
    QAction* newFromProfileAction = collection->addAction("new-from-profile");
    newFromProfileAction->setText( i18n("New from Profile...") );
    // TODO Implement "New from Profile"

158 159 160 161 162 163
    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()) ); 

164
       
165
#ifndef KONSOLE_PART 
166
    KStandardAction::quit( Application::self() , SLOT(quit()) , collection );
167
#endif
168 169

    // Bookmark Menu
170
    KActionMenu* bookmarkMenu = new KActionMenu(i18n("&Bookmarks") , collection );
171
    _bookmarkHandler = new BookmarkHandler( collection , bookmarkMenu->menu() , true , this );
172
    collection->addAction("bookmark" , bookmarkMenu);
173

174 175 176
    //TODO - The 'Add Bookmark' menu action currently has a Ctrl+B shortcut by
    // default which cannot be overridden

177 178 179 180 181
    // View Menu
    QAction* hideMenuBarAction = collection->addAction("hide-menubar");
    hideMenuBarAction->setText( i18n("Hide MenuBar") );
    connect( hideMenuBarAction , SIGNAL(triggered()) , menuBar() , SLOT(hide()) );

182 183 184 185
    QAction* mergeAction = collection->addAction("merge-windows");
    mergeAction->setText( i18n("&Merge Windows") );
    connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );

186
    // Settings Menu
187 188
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
189 190 191 192 193

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

194 195
}

196
BookmarkHandler* MainWindow::bookmarkHandler() const
197 198 199 200
{
    return _bookmarkHandler;
}

201
void MainWindow::setSessionList(ProfileList* list)
202
{
203
    sessionListChanged(list->actions());
204

205 206
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
207 208 209 210 211 212 213

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
214 215
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
216 217
}

218
void MainWindow::newTab()
219 220 221 222
{
    emit requestSession(QString(),_viewManager);
}

223
void MainWindow::newWindow()
224
{
225
#ifndef KONSOLE_PART
226
    Application::self()->newInstance();
227
#endif
228 229
}

230
void MainWindow::showShortcutsDialog()
231
{
232
    KShortcutsDialog::configure( actionCollection() );
233 234
}

235
void MainWindow::newFromProfile(const QString& key)
236 237 238
{
    emit requestSession(key,_viewManager);
}
239
void MainWindow::showManageProfilesDialog()
240
{
241
    ManageProfilesDialog dialog(this);
242 243
    dialog.exec();
}
244

245 246 247 248 249 250 251
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
        emit requestSession(dialog.sessionKey(),_viewManager);
}

252
void MainWindow::mergeWindows()
253
{
Robert Knight's avatar
Robert Knight committed
254 255 256 257
    // merges all of the open Konsole windows into this window
    // by merging the view manager associated with the other Konsole windows
    // into this window's view manager

258
    QListIterator<QWidget*> topLevelIter( QApplication::topLevelWidgets() );
259 260 261

    while (topLevelIter.hasNext())
    {
262
        MainWindow* window = qobject_cast<MainWindow*>(topLevelIter.next());
263 264 265 266 267 268
        if ( window && window != this )
        {
            _viewManager->merge( window->_viewManager );
            window->deleteLater();
        }
    }
269 270
}

271
void MainWindow::setupWidgets()
272
{
273 274 275 276 277
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

279 280 281 282
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
283

284 285 286
    widget->setLayout(layout);
    
    setCentralWidget(widget);
287
}
288

289
#include "MainWindow.moc"