]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinmainwindow.cpp
KFileItemModel: interface cleanups
[dolphin.git] / src / dolphinmainwindow.cpp
index 31628c763a846c5f797ee88c46adc261868c227f..aca4d15b68ba11831ae488455efc9280f36c89ee 100644 (file)
@@ -41,6 +41,7 @@
 #include "statusbar/dolphinstatusbar.h"
 #include "views/dolphinviewactionhandler.h"
 #include "views/dolphinremoteencoding.h"
+#include "views/draganddrophelper.h"
 #include "views/viewproperties.h"
 
 #ifndef Q_OS_WIN
@@ -102,18 +103,6 @@ namespace {
     const int CurrentDolphinVersion = 200;
 };
 
-/*
- * Menu shown when pressing the configure-button in the toolbar.
- */
-class ToolBarMenu : public KMenu
-{
-public:
-    ToolBarMenu(QWidget* parent);
-    virtual ~ToolBarMenu();
-protected:
-    virtual void showEvent(QShowEvent* event);
-};
-
 /*
  * Remembers the tab configuration if a tab has been closed.
  * Each closed tab can be restored by the menu
@@ -138,8 +127,7 @@ DolphinMainWindow::DolphinMainWindow() :
     m_actionHandler(0),
     m_remoteEncoding(0),
     m_settingsDialog(),
-    m_toolBarSpacer(0),
-    m_openToolBarMenuButton(0),
+    m_controlButton(0),
     m_updateToolBarTimer(0),
     m_lastHandleUrlStatJob(0),
     m_searchDockIsTemporaryVisible(false)
@@ -156,6 +144,8 @@ DolphinMainWindow::DolphinMainWindow() :
     setObjectName("Dolphin#");
 
     m_viewTab.append(ViewTab());
+    ViewTab& viewTab = m_viewTab[m_tabIndex];
+    viewTab.wasActive = true; // The first opened tab is automatically active
 
     KIO::FileUndoManager* undoManager = KIO::FileUndoManager::self();
     undoManager->setUiInterface(new UndoUiInterface());
@@ -177,8 +167,8 @@ DolphinMainWindow::DolphinMainWindow() :
 
     setAcceptDrops(true);
 
-    m_viewTab[m_tabIndex].splitter = new QSplitter(this);
-    m_viewTab[m_tabIndex].splitter->setChildrenCollapsible(false);
+    viewTab.splitter = new QSplitter(this);
+    viewTab.splitter->setChildrenCollapsible(false);
 
     setupActions();
 
@@ -188,9 +178,9 @@ DolphinMainWindow::DolphinMainWindow() :
     connect(m_actionHandler, SIGNAL(actionBeingHandled()), SLOT(clearStatusBar()));
     connect(m_actionHandler, SIGNAL(createDirectory()), SLOT(createDirectory()));
 
-    m_viewTab[m_tabIndex].primaryView = createViewContainer(homeUrl, m_viewTab[m_tabIndex].splitter);
+    viewTab.primaryView = createViewContainer(homeUrl, viewTab.splitter);
 
-    m_activeViewContainer = m_viewTab[m_tabIndex].primaryView;
+    m_activeViewContainer = viewTab.primaryView;
     connectViewSignals(m_activeViewContainer);
     DolphinView* view = m_activeViewContainer->view();
     m_activeViewContainer->show();
@@ -227,7 +217,7 @@ DolphinMainWindow::DolphinMainWindow() :
     m_centralWidgetLayout->setSpacing(0);
     m_centralWidgetLayout->setMargin(0);
     m_centralWidgetLayout->addWidget(m_tabBar);
-    m_centralWidgetLayout->addWidget(m_viewTab[m_tabIndex].splitter, 1);
+    m_centralWidgetLayout->addWidget(viewTab.splitter, 1);
 
     setCentralWidget(centralWidget);
     setupDockWidgets();
@@ -260,7 +250,7 @@ DolphinMainWindow::DolphinMainWindow() :
     QAction* showMenuBarAction = actionCollection()->action(KStandardAction::name(KStandardAction::ShowMenubar));
     showMenuBarAction->setChecked(showMenu);  // workaround for bug #171080
     if (!showMenu) {
-        createToolBarMenuButton();
+        createControlButton();
     }
 }
 
@@ -508,12 +498,12 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
 
     actionCollection()->action("close_tab")->setEnabled(true);
 
-    // provide a split view, if the startup settings are set this way
+    // Provide a split view, if the startup settings are set this way
     if (GeneralSettings::splitView()) {
-        const int tabIndex = m_viewTab.count() - 1;
-        createSecondaryView(tabIndex);
-        m_viewTab[tabIndex].secondaryView->setActive(true);
-        m_viewTab[tabIndex].isPrimaryViewActive = false;
+        const int newTabIndex = m_viewTab.count() - 1;
+        createSecondaryView(newTabIndex);
+        m_viewTab[newTabIndex].secondaryView->setActive(true);
+        m_viewTab[newTabIndex].isPrimaryViewActive = false;
     }
 
     if (focusWidget) {
@@ -525,25 +515,21 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
 
 void DolphinMainWindow::activateNextTab()
 {
-    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
-        return;
+    if (m_viewTab.count() >= 2) {
+        const int tabIndex = (m_tabBar->currentIndex() + 1) % m_tabBar->count();
+        m_tabBar->setCurrentIndex(tabIndex);
     }
-
-    const int tabIndex = (m_tabBar->currentIndex() + 1) % m_tabBar->count();
-    m_tabBar->setCurrentIndex(tabIndex);
 }
 
 void DolphinMainWindow::activatePrevTab()
 {
-    if ((m_viewTab.count() == 1) || (m_tabBar->count() < 2)) {
-        return;
-    }
-
-    int tabIndex = m_tabBar->currentIndex() - 1;
-    if (tabIndex == -1) {
-        tabIndex = m_tabBar->count() - 1;
+    if (m_viewTab.count() >= 2) {
+        int tabIndex = m_tabBar->currentIndex() - 1;
+        if (tabIndex == -1) {
+            tabIndex = m_tabBar->count() - 1;
+        }
+        m_tabBar->setCurrentIndex(tabIndex);
     }
-    m_tabBar->setCurrentIndex(tabIndex);
 }
 
 void DolphinMainWindow::openInNewTab()
@@ -1026,6 +1012,14 @@ void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
     }
 }
 
+void DolphinMainWindow::goHome(Qt::MouseButtons buttons)
+{
+    // The default case (left button pressed) is handled in goHome().
+    if (buttons == Qt::MidButton) {
+        openNewTab(GeneralSettings::self()->homeUrl());
+    }
+}
+
 void DolphinMainWindow::compareFiles()
 {
     // The method is only invoked if exactly 2 files have
@@ -1086,9 +1080,9 @@ void DolphinMainWindow::toggleShowMenuBar()
     const bool visible = menuBar()->isVisible();
     menuBar()->setVisible(!visible);
     if (visible) {
-        createToolBarMenuButton();
+        createControlButton();
     } else {
-        deleteToolBarMenuButton();
+        deleteControlButton();
     }
 }
 
@@ -1111,7 +1105,10 @@ void DolphinMainWindow::openTerminal()
 void DolphinMainWindow::editSettings()
 {
     if (!m_settingsDialog) {
-        const KUrl url = activeViewContainer()->url();
+        DolphinViewContainer* container = activeViewContainer();
+        container->view()->writeSettings();
+
+        const KUrl url = container->url();
         DolphinSettingsDialog* settingsDialog = new DolphinSettingsDialog(url, this);
         connect(settingsDialog, SIGNAL(settingsChanged()), this, SLOT(refreshViews()));
         settingsDialog->setAttribute(Qt::WA_DeleteOnClose);
@@ -1152,6 +1149,18 @@ void DolphinMainWindow::setActiveTab(int index)
     }
     viewTab.splitter->show();
 
+    if (!viewTab.wasActive) {
+        viewTab.wasActive = true;
+
+        // If the tab has not been activated yet the size of the KItemListView is
+        // undefined and results in an unwanted animation. To prevent this a
+        // reloading of the directory gets triggered.
+        viewTab.primaryView->view()->reload();
+        if (viewTab.secondaryView) {
+            viewTab.secondaryView->view()->reload();
+        }
+    }
+
     setActiveViewContainer(viewTab.isPrimaryViewActive ? viewTab.primaryView :
                                                          viewTab.secondaryView);
 }
@@ -1309,9 +1318,9 @@ void DolphinMainWindow::tabDropEvent(int tab, QDropEvent* event)
     const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
     if (!urls.isEmpty() && tab != -1) {
         const ViewTab& viewTab = m_viewTab[tab];
-        const KUrl destPath = viewTab.isPrimaryViewActive ? viewTab.primaryView->url() : viewTab.secondaryView->url();
-        Q_UNUSED(destPath);
-        //DragAndDropHelper::instance().dropUrls(KFileItem(), destPath, event, m_tabBar);
+        const DolphinView* view = viewTab.isPrimaryViewActive ? viewTab.primaryView->view()
+                                                              : viewTab.secondaryView->view();
+        DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event);
     }
 }
 
@@ -1393,7 +1402,7 @@ void DolphinMainWindow::openContextMenu(const QPoint& pos,
     delete contextMenu.data();
 }
 
-void DolphinMainWindow::updateToolBarMenu()
+void DolphinMainWindow::updateControlMenu()
 {
     KMenu* menu = qobject_cast<KMenu*>(sender());
     Q_ASSERT(menu);
@@ -1492,29 +1501,16 @@ void DolphinMainWindow::updateToolBarMenu()
 void DolphinMainWindow::updateToolBar()
 {
     if (!menuBar()->isVisible()) {
-        createToolBarMenuButton();
+        createControlButton();
     }
 }
 
-void DolphinMainWindow::slotToolBarSpacerDeleted()
+void DolphinMainWindow::slotControlButtonDeleted()
 {
-    m_toolBarSpacer = 0;
+    m_controlButton = 0;
     m_updateToolBarTimer->start();
 }
 
-void DolphinMainWindow::slotToolBarMenuButtonDeleted()
-{
-    m_openToolBarMenuButton = 0;
-    m_updateToolBarTimer->start();
-}
-
-void DolphinMainWindow::slotToolBarIconSizeChanged(const QSize& iconSize)
-{
-    if (m_openToolBarMenuButton) {
-        m_openToolBarMenuButton->setIconSize(iconSize);
-    }
-}
-
 void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
 {
     Q_ASSERT(viewContainer);
@@ -1680,7 +1676,8 @@ void DolphinMainWindow::setupActions()
     KAction* upAction = KStandardAction::up(this, SLOT(goUp()), actionCollection());
     connect(upAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goUp(Qt::MouseButtons)));
 
-    KStandardAction::home(this, SLOT(goHome()), actionCollection());
+    KAction* homeAction = KStandardAction::home(this, SLOT(goHome()), actionCollection());
+    connect(homeAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goHome(Qt::MouseButtons)));
 
     // setup 'Tools' menu
     KAction* showFilterBar = actionCollection()->addAction("show_filter_bar");
@@ -1872,14 +1869,15 @@ void DolphinMainWindow::setupDockWidgets()
     KActionMenu* panelsMenu = new KActionMenu(i18nc("@action:inmenu View", "Panels"), this);
     actionCollection()->addAction("panels", panelsMenu);
     panelsMenu->setDelayed(false);
-    panelsMenu->addAction(placesAction);
-    panelsMenu->addAction(infoAction);
-    panelsMenu->addAction(foldersAction);
+    const KActionCollection* ac = actionCollection();
+    panelsMenu->addAction(ac->action("show_places_panel"));
+    panelsMenu->addAction(ac->action("show_information_panel"));
+    panelsMenu->addAction(ac->action("show_folders_panel"));
 #ifndef Q_OS_WIN
-    panelsMenu->addAction(terminalAction);
+    panelsMenu->addAction(ac->action("show_terminal_panel"));
 #endif
 #ifdef HAVE_NEPOMUK
-    panelsMenu->addAction(searchAction);
+    panelsMenu->addAction(ac->action("show_search_panel"));
 #endif
     panelsMenu->addSeparator();
     panelsMenu->addAction(lockLayoutAction);
@@ -1933,48 +1931,43 @@ void DolphinMainWindow::updateGoActions()
     goUpAction->setEnabled(currentUrl.upUrl() != currentUrl);
 }
 
-void DolphinMainWindow::createToolBarMenuButton()
+void DolphinMainWindow::createControlButton()
 {
-    if (m_toolBarSpacer && m_openToolBarMenuButton) {
+    if (m_controlButton) {
         return;
     }
-    Q_ASSERT(!m_toolBarSpacer);
-    Q_ASSERT(!m_openToolBarMenuButton);
-
-    m_toolBarSpacer = new QWidget(this);
-    m_toolBarSpacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
+    Q_ASSERT(!m_controlButton);
 
-    m_openToolBarMenuButton = new QToolButton(this);
-    m_openToolBarMenuButton->setIcon(KIcon("configure"));
-    m_openToolBarMenuButton->setPopupMode(QToolButton::InstantPopup);
-    m_openToolBarMenuButton->setToolTip(i18nc("@info:tooltip", "Configure and control Dolphin"));
+    m_controlButton = new QToolButton(this);
+    m_controlButton->setIcon(KIcon("applications-system"));
+    m_controlButton->setText(i18nc("@action", "Control"));
+    m_controlButton->setPopupMode(QToolButton::InstantPopup);
+    m_controlButton->setToolButtonStyle(toolBar()->toolButtonStyle());
 
-    KMenu* toolBarMenu = new ToolBarMenu(m_openToolBarMenuButton);
-    connect(toolBarMenu, SIGNAL(aboutToShow()), this, SLOT(updateToolBarMenu()));
+    KMenu* controlMenu = new KMenu(m_controlButton);
+    connect(controlMenu, SIGNAL(aboutToShow()), this, SLOT(updateControlMenu()));
 
-    m_openToolBarMenuButton->setMenu(toolBarMenu);
+    m_controlButton->setMenu(controlMenu);
 
-    toolBar()->addWidget(m_toolBarSpacer);
-    toolBar()->addWidget(m_openToolBarMenuButton);
-    connect(toolBar(), SIGNAL(iconSizeChanged(QSize)), this, SLOT(slotToolBarIconSizeChanged(QSize)));
+    toolBar()->addWidget(m_controlButton);
+    connect(toolBar(), SIGNAL(iconSizeChanged(QSize)),
+            m_controlButton, SLOT(setIconSize(QSize)));
+    connect(toolBar(), SIGNAL(toolButtonStyleChanged(Qt::ToolButtonStyle)),
+            m_controlButton, SLOT(setToolButtonStyle(Qt::ToolButtonStyle)));
 
     // The added widgets are owned by the toolbar and may get deleted when e.g. the toolbar
     // gets edited. In this case we must add them again. The adding is done asynchronously by
     // m_updateToolBarTimer.
-    connect(m_toolBarSpacer, SIGNAL(destroyed()), this, SLOT(slotToolBarSpacerDeleted()));
-    connect(m_openToolBarMenuButton, SIGNAL(destroyed()), this, SLOT(slotToolBarMenuButtonDeleted()));
+    connect(m_controlButton, SIGNAL(destroyed()), this, SLOT(slotControlButtonDeleted()));
     m_updateToolBarTimer = new QTimer(this);
     m_updateToolBarTimer->setInterval(500);
     connect(m_updateToolBarTimer, SIGNAL(timeout()), this, SLOT(updateToolBar()));
 }
 
-void DolphinMainWindow::deleteToolBarMenuButton()
+void DolphinMainWindow::deleteControlButton()
 {
-    delete m_toolBarSpacer;
-    m_toolBarSpacer = 0;
-
-    delete m_openToolBarMenuButton;
-    m_openToolBarMenuButton = 0;
+    delete m_controlButton;
+    m_controlButton = 0;
 
     delete m_updateToolBarTimer;
     m_updateToolBarTimer = 0;
@@ -2045,9 +2038,9 @@ void DolphinMainWindow::refreshViews()
 
     const int tabCount = m_viewTab.count();
     for (int i = 0; i < tabCount; ++i) {
-        m_viewTab[i].primaryView->refresh();
+        m_viewTab[i].primaryView->readSettings();
         if (m_viewTab[i].secondaryView) {
-            m_viewTab[i].secondaryView->refresh();
+            m_viewTab[i].secondaryView->readSettings();
         }
     }
 
@@ -2095,10 +2088,14 @@ void DolphinMainWindow::connectViewSignals(DolphinViewContainer* container)
             this, SLOT(openNewTab(KUrl)));
     connect(view, SIGNAL(requestContextMenu(QPoint,KFileItem,KUrl,QList<QAction*>)),
             this, SLOT(openContextMenu(QPoint,KFileItem,KUrl,QList<QAction*>)));
-    connect(view, SIGNAL(startedPathLoading(KUrl)),
+    connect(view, SIGNAL(directoryLoadingStarted()),
             this, SLOT(enableStopAction()));
-    connect(view, SIGNAL(finishedPathLoading(KUrl)),
+    connect(view, SIGNAL(directoryLoadingCompleted()),
             this, SLOT(disableStopAction()));
+    connect(view, SIGNAL(goBackRequested()),
+            this, SLOT(goBack()));
+    connect(view, SIGNAL(goForwardRequested()),
+            this, SLOT(goForward()));
 
     const KUrlNavigator* navigator = container->urlNavigator();
     connect(navigator, SIGNAL(urlChanged(KUrl)),
@@ -2164,16 +2161,20 @@ bool DolphinMainWindow::isKompareInstalled() const
 
 void DolphinMainWindow::createSecondaryView(int tabIndex)
 {
-    QSplitter* splitter = m_viewTab[tabIndex].splitter;
-    const int newWidth = (m_viewTab[tabIndex].primaryView->width() - splitter->handleWidth()) / 2;
+    ViewTab& viewTab = m_viewTab[tabIndex];
 
-    const DolphinView* view = m_viewTab[tabIndex].primaryView->view();
-    m_viewTab[tabIndex].secondaryView = createViewContainer(view->url(), 0);
-    splitter->addWidget(m_viewTab[tabIndex].secondaryView);
+    QSplitter* splitter = viewTab.splitter;
+    const int newWidth = (viewTab.primaryView->width() - splitter->handleWidth()) / 2;
+
+    const DolphinView* view = viewTab.primaryView->view();
+    viewTab.secondaryView = createViewContainer(view->url(), 0);
+    splitter->addWidget(viewTab.secondaryView);
     splitter->setSizes(QList<int>() << newWidth << newWidth);
-    connectViewSignals(m_viewTab[tabIndex].secondaryView);
-    m_viewTab[tabIndex].secondaryView->setActive(false);
-    m_viewTab[tabIndex].secondaryView->show();
+
+    connectViewSignals(viewTab.secondaryView);
+    viewTab.secondaryView->setActive(false);
+    viewTab.secondaryView->resize(newWidth, viewTab.primaryView->height());
+    viewTab.secondaryView->show();
 }
 
 QString DolphinMainWindow::tabProperty(const QString& property, int tabIndex) const
@@ -2209,12 +2210,14 @@ void DolphinMainWindow::createPanelAction(const KIcon& icon,
                                           const QString& actionName)
 {
     KAction* panelAction = actionCollection()->addAction(actionName);
+    panelAction->setCheckable(true);
+    panelAction->setChecked(dockAction->isChecked());
     panelAction->setText(dockAction->text());
     panelAction->setIcon(icon);
     panelAction->setShortcut(shortcut);
 
-    dockAction->setIcon(icon);
     connect(panelAction, SIGNAL(triggered()), dockAction, SLOT(trigger()));
+    connect(dockAction, SIGNAL(toggled(bool)), panelAction, SLOT(setChecked(bool)));
 }
 
 DolphinMainWindow::UndoUiInterface::UndoUiInterface() :
@@ -2237,47 +2240,4 @@ void DolphinMainWindow::UndoUiInterface::jobError(KIO::Job* job)
     }
 }
 
-ToolBarMenu::ToolBarMenu(QWidget* parent) :
-    KMenu(parent)
-{
-}
-
-ToolBarMenu::~ToolBarMenu()
-{
-}
-
-void ToolBarMenu::showEvent(QShowEvent* event)
-{
-    KMenu::showEvent(event);
-
-    // Adjust the position of the menu to be shown within the
-    // Dolphin window to reduce the cases that sub-menus might overlap
-    // the right screen border.
-    QPoint pos;
-    QWidget* button = parentWidget();
-    if (layoutDirection() == Qt::RightToLeft) {
-        pos = button->mapToGlobal(QPoint(0, button->height()));
-    } else {
-        pos = button->mapToGlobal(QPoint(button->width(), button->height()));
-        pos.rx() -= width();
-    }
-
-    // Assure that the menu is not shown outside the screen boundaries and
-    // that it does not overlap with the parent button.
-    const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
-    if (pos.x() < screen.x()) {
-        pos.rx() = screen.x();
-    } else if (pos.x() + width() > screen.x() + screen.width()) {
-        pos.rx() = screen.x() + screen.width() - width();
-    }
-
-    if (pos.y() < screen.y()) {
-        pos.ry() = screen.y();
-    } else if (pos.y() + height() > screen.y() + screen.height()) {
-        pos.ry() = button->mapToGlobal(QPoint(0, 0)).y() - height();
-    }
-
-    move(pos);
-}
-
 #include "dolphinmainwindow.moc"