]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/urlnavigatorbutton.cpp
Allow zooming in and zooming out in the icons view.
[dolphin.git] / src / urlnavigatorbutton.cpp
index 811a11b89246035076df2c900b866135e5064d0b..3be037ced8d6b5d876e75aa78803e47f6e32bad4 100644 (file)
  *   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.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
  ***************************************************************************/
 
 #include "urlnavigatorbutton.h"
-#include <qcursor.h>
-#include <qfontmetrics.h>
-#include <qpainter.h>
-#include <qtimer.h>
-#include <qtooltip.h>
-//Added by qt3to4:
-#include <QDropEvent>
-#include <QDragLeaveEvent>
-#include <Q3PopupMenu>
-#include <QEvent>
-#include <QDragEnterEvent>
 
-#include <kglobalsettings.h>
-#include <kiconloader.h>
-#include <kio/jobclasses.h>
-#include <klocale.h>
-#include <kurl.h>
 #include <assert.h>
 
 #include "urlnavigator.h"
-#include "dolphinview.h"
-#include "dolphin.h"
+
+#include <kio/job.h>
+#include <kio/jobclasses.h>
+#include <kglobalsettings.h>
+#include <kmenu.h>
+
+#include <QPainter>
+#include <QPaintEvent>
+#include <QTimer>
 
 UrlNavigatorButton::UrlNavigatorButton(int index, UrlNavigator* parent) :
     UrlButton(parent),
     m_index(-1),
+    m_popupDelay(0),
     m_listJob(0)
 {
     setAcceptDrops(true);
@@ -53,6 +45,7 @@ UrlNavigatorButton::UrlNavigatorButton(int index, UrlNavigator* parent) :
     connect(this, SIGNAL(clicked()), this, SLOT(updateNavigatorUrl()));
 
     m_popupDelay = new QTimer(this);
+    m_popupDelay->setSingleShot(true);
     connect(m_popupDelay, SIGNAL(timeout()), this, SLOT(startListJob()));
     connect(this, SIGNAL(pressed()), this, SLOT(startPopupDelay()));
 }
@@ -89,13 +82,16 @@ void UrlNavigatorButton::setIndex(int index)
     update();
 }
 
-int UrlNavigatorButton::index() const
+QSize UrlNavigatorButton::sizeHint() const
 {
-    return m_index;
+    const int width = fontMetrics().width(text()) + (arrowWidth() * 4);
+    return QSize(width, UrlButton::sizeHint().height());
 }
 
-void UrlNavigatorButton::drawButton(QPainter* painter)
+void UrlNavigatorButton::paintEvent(QPaintEvent* event)
 {
+    QPainter painter(this);
+    painter.setClipRect(event->rect());
     const int buttonWidth  = width();
     const int buttonHeight = height();
 
@@ -109,17 +105,14 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
         foregroundColor = KGlobalSettings::highlightedTextColor();
     }
     else {
-        backgroundColor = colorGroup().background();
+        backgroundColor = palette().brush(QPalette::Background).color();
         foregroundColor = KGlobalSettings::buttonTextColor();
     }
 
     // dimm the colors if the parent view does not have the focus
-    const DolphinView* parentView = urlNavigator()->dolphinView();
-    const Dolphin& dolphin = Dolphin::mainWin();
-
-    const bool isActive = (dolphin.activeView() == parentView);
+    const bool isActive = urlNavigator()->isActive();
     if (!isActive) {
-        QColor dimmColor(colorGroup().background());
+        QColor dimmColor(palette().brush(QPalette::Background).color());
         foregroundColor = mixColors(foregroundColor, dimmColor);
         if (isHighlighted) {
             backgroundColor = mixColors(backgroundColor, dimmColor);
@@ -127,18 +120,18 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
     }
 
     // draw button background
-    painter->setPen(Qt::NoPen);
-    painter->setBrush(backgroundColor);
-    painter->drawRect(0, 0, buttonWidth, buttonHeight);
+    painter.setPen(Qt::NoPen);
+    painter.setBrush(backgroundColor);
+    painter.drawRect(0, 0, buttonWidth, buttonHeight);
 
     int textWidth = buttonWidth;
     if (isDisplayHintEnabled(ActivatedHint) && isActive || isHighlighted) {
-        painter->setPen(foregroundColor);
+        painter.setPen(foregroundColor);
     }
     else {
         // dimm the foreground color by mixing it with the background
         foregroundColor = mixColors(foregroundColor, backgroundColor);
-        painter->setPen(foregroundColor);
+        painter.setPen(foregroundColor);
     }
 
     if (!isDisplayHintEnabled(ActivatedHint)) {
@@ -150,8 +143,8 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
         const int startTopY = middleY - (width - 1);
         const int startBottomY = middleY + (width - 1);
         for (int i = 0; i < width; ++i) {
-            painter->drawLine(startX, startTopY + i, startX + i, startTopY + i);
-            painter->drawLine(startX, startBottomY - i, startX + i, startBottomY - i);
+            painter.drawLine(startX, startTopY + i, startX + i, startTopY + i);
+            painter.drawLine(startX, startBottomY - i, startX + i, startBottomY - i);
         }
 
         textWidth = startX - border;
@@ -159,7 +152,7 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
 
     const bool clipped = isTextClipped();
     const int align = clipped ? Qt::AlignVCenter : Qt::AlignCenter;
-    painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text());
+    painter.drawText(QRect(0, 0, textWidth, buttonHeight), align, text());
 
     if (clipped) {
         // Blend the right area of the text with the background, as the
@@ -172,9 +165,9 @@ void UrlNavigatorButton::drawButton(QPainter* painter)
         const int greenInc = (foregroundColor.green() - backgroundColor.green()) / blendSteps;
         const int blueInc  = (foregroundColor.blue()  - backgroundColor.blue())  / blendSteps;
         for (int i = 0; i < blendSteps; ++i) {
-            painter->setClipRect(QRect(textWidth - i, 0, 1, buttonHeight));
-            painter->setPen(blendColor);
-            painter->drawText(QRect(0, 0, textWidth, buttonHeight), align, text());
+            painter.setClipRect(QRect(textWidth - i, 0, 1, buttonHeight));
+            painter.setPen(blendColor);
+            painter.drawText(QRect(0, 0, textWidth, buttonHeight), align, text());
 
             blendColor.setRgb(blendColor.red()   + redInc,
                               blendColor.green() + greenInc,
@@ -190,14 +183,14 @@ void UrlNavigatorButton::enterEvent(QEvent* event)
     // if the text is clipped due to a small window width, the text should
     // be shown as tooltip
     if (isTextClipped()) {
-        QToolTip::add(this, text());
+        setToolTip(text());
     }
 }
 
 void UrlNavigatorButton::leaveEvent(QEvent* event)
 {
     UrlButton::leaveEvent(event);
-    QToolTip::remove(this);
+    setToolTip(QString());
 }
 
 void UrlNavigatorButton::dropEvent(QDropEvent* event)
@@ -206,28 +199,30 @@ void UrlNavigatorButton::dropEvent(QDropEvent* event)
         return;
     }
 
-    KUrl::List urls;
-    /* KDE4-TODO:
-    if (KUrlDrag::decode(event, urls) && !urls.isEmpty()) {
+    const KUrl::List urls = KUrl::List::fromMimeData(event->mimeData());
+    if (!urls.isEmpty()) {
+        event->acceptProposedAction();
+
         setDisplayHintEnabled(DraggedHint, true);
 
         QString path(urlNavigator()->url().prettyUrl());
-        path = path.section('/', 0, m_index);
+        path = path.section('/', 0, m_index + 2);
 
-        Dolphin::mainWin().dropUrls(urls, KUrl(path));
+        urlNavigator()->dropUrls(urls, KUrl(path));
 
         setDisplayHintEnabled(DraggedHint, false);
         update();
-    }*/
+    }
 }
 
 void UrlNavigatorButton::dragEnterEvent(QDragEnterEvent* event)
 {
-    /* KDE4-TODO:
-    event->accept(KUrlDrag::canDecode(event));
+    if (event->mimeData()->hasUrls()) {
+        setDisplayHintEnabled(DraggedHint, true);
+        event->acceptProposedAction();
 
-    setDisplayHintEnabled(DraggedHint, true);*/
-    update();
+        update();
+    }
 }
 
 void UrlNavigatorButton::dragLeaveEvent(QDragLeaveEvent* event)
@@ -241,28 +236,28 @@ void UrlNavigatorButton::dragLeaveEvent(QDragLeaveEvent* event)
 
 void UrlNavigatorButton::updateNavigatorUrl()
 {
+    stopPopupDelay();
+
     if (m_index < 0) {
         return;
     }
 
-    UrlNavigator* navigator = urlNavigator();
-    assert(navigator != 0);
-    navigator->setUrl(navigator->url(m_index));
+    urlNavigator()->setUrl(urlNavigator()->url(m_index));
 }
 
 void UrlNavigatorButton::startPopupDelay()
 {
-    if (m_popupDelay->isActive() || m_listJob || m_index < 0) {
+    if (m_popupDelay->isActive() || (m_listJob != 0) || (m_index < 0)) {
         return;
     }
 
-    m_popupDelay->start(300, true);
+    m_popupDelay->start(300);
 }
 
 void UrlNavigatorButton::stopPopupDelay()
 {
     m_popupDelay->stop();
-    if (m_listJob) {
+    if (m_listJob != 0) {
         m_listJob->kill();
         m_listJob = 0;
     }
@@ -270,17 +265,17 @@ void UrlNavigatorButton::stopPopupDelay()
 
 void UrlNavigatorButton::startListJob()
 {
-    if (m_listJob) {
+    if (m_listJob != 0) {
         return;
     }
 
-    KUrl url = urlNavigator()->url(m_index);
+    const KUrl& url = urlNavigator()->url(m_index);
     m_listJob = KIO::listDir(url, false, false);
     m_subdirs.clear(); // just to be ++safe
 
     connect(m_listJob, SIGNAL(entries(KIO::Job*, const KIO::UDSEntryList &)),
             this, SLOT(entriesList(KIO::Job*, const KIO::UDSEntryList&)));
-    connect(m_listJob, SIGNAL(result(KIO::Job*)), this, SLOT(listJobFinished(KIO::Job*)));
+    connect(m_listJob, SIGNAL(result(KJob*)), this, SLOT(listJobFinished(KJob*)));
 }
 
 void UrlNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& entries)
@@ -293,7 +288,7 @@ void UrlNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& ent
     KIO::UDSEntryList::const_iterator itEnd = entries.constEnd();
     while (it != itEnd) {
         QString name;
-        bool isDir = false;
+        //bool isDir = false;
         KIO::UDSEntry entry = *it;
 
         /* KDE3 reference:
@@ -328,7 +323,7 @@ void UrlNavigatorButton::entriesList(KIO::Job* job, const KIO::UDSEntryList& ent
     m_subdirs.sort();
 }
 
-void UrlNavigatorButton::listJobFinished(KIO::Job* job)
+void UrlNavigatorButton::listJobFinished(KJob* job)
 {
     if (job != m_listJob) {
         return;
@@ -341,20 +336,22 @@ void UrlNavigatorButton::listJobFinished(KIO::Job* job)
 
     setDisplayHintEnabled(PopupActiveHint, true);
     update(); // ensure the button is drawn highlighted
-    Q3PopupMenu* dirsMenu = new Q3PopupMenu(this);
-    //setPopup(dirsMenu);
+
+    KMenu* dirsMenu = new KMenu(this);
     QStringList::const_iterator it = m_subdirs.constBegin();
     QStringList::const_iterator itEnd = m_subdirs.constEnd();
     int i = 0;
     while (it != itEnd) {
-        dirsMenu->insertItem(*it, i);
-        ++i;
+        QAction* action = new QAction(*it, this);
+        action->setData(i);
+        dirsMenu->addAction(action);
         ++it;
+        ++i;
     }
 
-    int result = dirsMenu->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
-
-    if (result != -1) {
+    const QAction* action = dirsMenu->exec(urlNavigator()->mapToGlobal(geometry().bottomLeft()));
+    if (action != 0) {
+        const int result = action->data().toInt();
         KUrl url = urlNavigator()->url(m_index);
         url.addPath(m_subdirs[result]);
         urlNavigator()->setUrl(url);
@@ -363,6 +360,8 @@ void UrlNavigatorButton::listJobFinished(KIO::Job* job)
     m_listJob = 0;
     m_subdirs.clear();
     delete dirsMenu;
+    dirsMenu = 0;
+
     setDisplayHintEnabled(PopupActiveHint, false);
 }