From: Peter Penz Date: Sat, 17 Feb 2007 18:25:46 +0000 (+0000) Subject: Allow the pasting of a text clipboard content to the URL navigator by pressing the... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/4bf793794fd3842b857acde4af5db83be3a1f15d Allow the pasting of a text clipboard content to the URL navigator by pressing the middle mouse button. Although this is quite a hidden feature, I think it makes happy a lot of people who just want to paste a URL available as text to the URL navigator without switching from the breadcrump view to the traditional view. Very nice "side effect": the URL navigator is now completely flicker free when changing URLs because of using the whole available width :-) TODO: I'm not 100 % sure whether checking the middle mousebutton by 'if (event->button() == Qt::MidButton) { ... }' is the right approach (maybe there is a more generic way to check the 'paste button'). svn path=/trunk/KDE/kdebase/apps/; revision=634595 --- diff --git a/src/urlnavigator.cpp b/src/urlnavigator.cpp index d700be2a0..c30e6ed55 100644 --- a/src/urlnavigator.cpp +++ b/src/urlnavigator.cpp @@ -35,10 +35,14 @@ #include #include -#include +#include +#include #include +#include +#include #include #include +#include UrlNavigator::HistoryElem::HistoryElem() : m_url(), @@ -62,21 +66,24 @@ UrlNavigator::HistoryElem::~HistoryElem() UrlNavigator::UrlNavigator(const KUrl& url, QWidget* parent) : - KHBox(parent), + QWidget(parent), m_active(true), m_historyIndex(0), + m_layout(0), m_protocols(0), m_protocolSeparator(0), m_host(0) { + m_layout = new QHBoxLayout(); + m_layout->setSpacing(0); + m_layout->setMargin(0); + m_history.prepend(HistoryElem(url)); QFontMetrics fontMetrics(font()); setMinimumHeight(fontMetrics.height() + 8); - m_toggleButton = new QCheckBox(this); - //m_toggleButton->setFlat(true); - //m_toggleButton->setToggleButton(true); + m_toggleButton = new QCheckBox(); m_toggleButton->setFocusPolicy(Qt::NoFocus); m_toggleButton->setMinimumHeight(minimumHeight()); connect(m_toggleButton, SIGNAL(clicked()), @@ -102,6 +109,19 @@ UrlNavigator::UrlNavigator(const KUrl& url, //connect(dolphinView, SIGNAL(redirection(const KUrl&, const KUrl&)), // this, SLOT(slotRedirection(const KUrl&, const KUrl&))); + + // Append a filler widget at the end, which automatically resizes to the + // maximum available width. This assures that the URL navigator uses the + // whole width, so that the clipboard content can be dropped. + QWidget* filler = new QWidget(); + filler->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + + m_layout->addWidget(m_toggleButton); + m_layout->addWidget(m_bookmarkSelector); + m_layout->addWidget(m_pathBox); + m_layout->addWidget(filler); + setLayout(m_layout); + updateContent(); } @@ -284,12 +304,25 @@ void UrlNavigator::storeContentsPosition(int x, int y) void UrlNavigator::keyReleaseEvent(QKeyEvent* event) { - KHBox::keyReleaseEvent(event); + QWidget::keyReleaseEvent(event); if (isUrlEditable() && (event->key() == Qt::Key_Escape)) { setUrlEditable(false); } } +void UrlNavigator::mouseReleaseEvent(QMouseEvent* event) +{ + if (event->button() == Qt::MidButton) { + QClipboard* clipboard = QApplication::clipboard(); + const QMimeData* mimeData = clipboard->mimeData(); + if (mimeData->hasText()) { + const QString text = mimeData->text(); + setUrl(KUrl(text)); + } + } + QWidget::mouseReleaseEvent(event); +} + void UrlNavigator::slotReturnPressed(const QString& text) { // Parts of the following code have been taken @@ -467,7 +500,7 @@ void UrlNavigator::updateContent() else { m_toggleButton->setToolTip(i18n("Edit location (%1)", shortcut)); - setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); m_pathBox->hide(); // get the data from the currently selected bookmark @@ -525,7 +558,9 @@ void UrlNavigator::updateContent() if (!m_host) { m_protocolSeparator = new QLabel("://", this); + appendWidget(m_protocolSeparator); m_host = new QLineEdit(hostText, this); + appendWidget(m_host); connect(m_host, SIGNAL(lostFocus()), this, SLOT(slotRemoteHostActivated())); @@ -591,6 +626,7 @@ void UrlNavigator::updateButtons(const QString& path, int startIndex) UrlNavigatorButton* button = 0; if (createButton) { button = new UrlNavigatorButton(idx, this); + appendWidget(button); } else { button = *it; @@ -635,4 +671,9 @@ void UrlNavigator::deleteButtons() m_navButtons.erase(itBegin, itEnd); } +void UrlNavigator::appendWidget(QWidget* widget) +{ + m_layout->insertWidget(m_layout->count() - 1, widget); +} + #include "urlnavigator.moc" diff --git a/src/urlnavigator.h b/src/urlnavigator.h index b2e1da90e..6a3d7d579 100644 --- a/src/urlnavigator.h +++ b/src/urlnavigator.h @@ -22,13 +22,15 @@ #ifndef URLNAVIGATOR_H #define URLNAVIGATOR_H -#include #include +#include #include +class QCheckBox; +class QHBoxLayout; class QLabel; class QLineEdit; -class QCheckBox; +class QMouseEvent; class KUrl; class KFileItem; @@ -58,7 +60,7 @@ class ProtocolCombo; typedef QLinkedList UrlStack; -class UrlNavigator : public KHBox +class UrlNavigator : public QWidget { Q_OBJECT @@ -221,10 +223,18 @@ signals: const KUrl& destination); protected: - /** If the Escape key is pressed, the navigation bar should switch - to the browse mode. */ + /** + * If the Escape key is pressed, the navigation bar should switch + * to the browse mode. + */ virtual void keyReleaseEvent(QKeyEvent* event); + /** + * Paste the clipboard content as URL, if the middle mouse + * button has been clicked. + */ + virtual void mouseReleaseEvent(QMouseEvent* event); + private slots: void slotReturnPressed(const QString& text); void slotUrlActivated(const KUrl& url); @@ -270,9 +280,19 @@ private: */ void deleteButtons(); + /** + * Appends the widget at the end of the URL navigator. It is assured + * that the filler widget remains as last widget to fill the remaining + * width. + */ + void appendWidget(QWidget* widget); + private: bool m_active; int m_historyIndex; + + QHBoxLayout* m_layout; + QLinkedList m_history; QCheckBox* m_toggleButton; BookmarkSelector* m_bookmarkSelector;