MainWindow.cpp 9.32 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 25
#include <QtGui/QApplication>
#include <QtGui/QBoxLayout>
26

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

// Konsole
41 42 43 44
#ifndef KONSOLE_PART
    #include "Application.h"
#endif

45
#include "BookmarkHandler.h"
46 47
#include "IncrementalSearchBar.h"
#include "RemoteConnectionDialog.h"
48
#include "SessionController.h"
49 50
#include "ProfileList.h"
#include "ManageProfilesDialog.h"
51 52
#include "ViewManager.h"
#include "ViewSplitter.h"
53

54 55
using namespace Konsole;

56
MainWindow::MainWindow()
57
 : KXmlGuiWindow() ,
58 59
   _bookmarkHandler(0),
   _pluggedController(0)
60
{
61 62 63
    // 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
64
    //setContentsMargins(0,2,0,0);
65
   
66
    // create actions for menus
67
    setXMLFile("konsole/konsoleui.rc");
68 69 70
    setupActions();

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

81 82 83
    connect( _viewManager , SIGNAL(setMenuBarVisible(bool)) , menuBar() ,
            SLOT(setVisible(bool)) );

84 85 86
    // create main window widgets
    setupWidgets();

87 88 89 90
    // create menus
    createGUI();
}

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

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
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*)) );
115
        guiFactory()->removeClient(_pluggedController);
116 117
    }

118 119
    Q_ASSERT( controller );

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

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

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

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

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

159
    QAction* newFromProfileAction = collection->addAction("new-from-profile");
Stephan Binner's avatar
Stephan Binner committed
160
    newFromProfileAction->setText( i18n("New From Profile...") );
161 162
    // TODO Implement "New from Profile"

163 164 165 166 167 168
    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()) ); 

169
       
170
#ifndef KONSOLE_PART 
171
    KStandardAction::quit( Application::self() , SLOT(quit()) , collection );
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 183 184 185 186
    // View Menu
    QAction* hideMenuBarAction = collection->addAction("hide-menubar");
    hideMenuBarAction->setText( i18n("Hide MenuBar") );
    connect( hideMenuBarAction , SIGNAL(triggered()) , menuBar() , SLOT(hide()) );

187 188 189 190 191
    //TODO - Implmement this correctly
    //
    //QAction* mergeAction = collection->addAction("merge-windows");
    //mergeAction->setText( i18n("&Merge Windows") );
    //connect( mergeAction , SIGNAL(triggered()) , this , SLOT(mergeWindows()) );
192

193
    // Settings Menu
194 195
    KStandardAction::configureNotifications( 0 , 0 , collection  );
    KStandardAction::keyBindings( this , SLOT(showShortcutsDialog()) , collection  );
196 197 198 199 200

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

201 202
}

203
BookmarkHandler* MainWindow::bookmarkHandler() const
204 205 206 207
{
    return _bookmarkHandler;
}

208
void MainWindow::setSessionList(ProfileList* list)
209
{
210
    sessionListChanged(list->actions());
211

212 213
    connect( list , SIGNAL(profileSelected(const QString&)) , this , 
            SLOT(newFromProfile(const QString&)) );
214 215 216 217 218 219 220

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

void MainWindow::sessionListChanged(const QList<QAction*>& actions)
{
221 222
    unplugActionList("favorite-profiles");
    plugActionList("favorite-profiles",actions);
223 224
}

225
void MainWindow::newTab()
226 227 228 229
{
    emit requestSession(QString(),_viewManager);
}

230
void MainWindow::newWindow()
231
{
232
#ifndef KONSOLE_PART
233
    Application::self()->newInstance();
234
#endif
235 236
}

237
void MainWindow::showShortcutsDialog()
238
{
239
    KShortcutsDialog::configure( actionCollection() );
240 241
}

242
void MainWindow::newFromProfile(const QString& key)
243 244 245
{
    emit requestSession(key,_viewManager);
}
246
void MainWindow::showManageProfilesDialog()
247
{
248 249
    ManageProfilesDialog* dialog = new ManageProfilesDialog(this);
    dialog->show();
250
}
251

252 253 254 255 256 257 258
void MainWindow::showRemoteConnectionDialog()
{
    RemoteConnectionDialog dialog(this);
    if ( dialog.exec() == QDialog::Accepted )
        emit requestSession(dialog.sessionKey(),_viewManager);
}

259
void MainWindow::mergeWindows()
260
{
Robert Knight's avatar
Robert Knight committed
261 262 263 264
    // 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

265
    QListIterator<QWidget*> topLevelIter( QApplication::topLevelWidgets() );
266 267 268

    while (topLevelIter.hasNext())
    {
269 270 271
        QWidget* w = topLevelIter.next();
        qDebug() << "Top level widget: " << w->metaObject()->className();
        MainWindow* window = qobject_cast<MainWindow*>(w);
272 273 274
        if ( window && window != this )
        {
            _viewManager->merge( window->_viewManager );
275
            //window->close();
276 277
        }
    }
278 279
}

280
void MainWindow::setupWidgets()
281
{
282 283 284 285 286
    QWidget* widget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout();

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

288 289 290 291
    layout->addWidget( _viewManager->widget() );
    layout->addWidget( _searchBar );
    layout->setMargin(0);
    layout->setSpacing(0);
292

293 294 295
    widget->setLayout(layout);
    
    setCentralWidget(widget);
296
}
297

298
#include "MainWindow.moc"