2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2010-2014 David Rosca <nowrep@gmail.com>
|
2011-03-03 18:29:20 +01:00
|
|
|
*
|
|
|
|
* 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-03-02 16:57:41 +01:00
|
|
|
#include "bookmarkstoolbar.h"
|
2014-02-10 11:54:58 +01:00
|
|
|
#include "bookmarkstoolbarbutton.h"
|
|
|
|
#include "bookmarkstools.h"
|
|
|
|
#include "bookmarkitem.h"
|
2012-01-21 23:19:38 +01:00
|
|
|
#include "mainapplication.h"
|
2014-02-05 15:26:51 +01:00
|
|
|
#include "bookmarks.h"
|
2014-02-10 11:54:58 +01:00
|
|
|
|
|
|
|
#include <QDragEnterEvent>
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QMimeData>
|
2014-02-10 11:54:58 +01:00
|
|
|
#include <QTimer>
|
|
|
|
#include <QFrame>
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2014-02-19 22:07:21 +01:00
|
|
|
BookmarksToolbar::BookmarksToolbar(BrowserWindow* window, QWidget* parent)
|
2011-10-28 17:52:42 +02:00
|
|
|
: QWidget(parent)
|
2014-02-19 22:07:21 +01:00
|
|
|
, m_window(window)
|
2014-02-05 15:26:51 +01:00
|
|
|
, m_bookmarks(mApp->bookmarks())
|
2014-02-10 11:54:58 +01:00
|
|
|
, m_clickedBookmark(0)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
setObjectName("bookmarksbar");
|
2014-02-10 11:54:58 +01:00
|
|
|
setAcceptDrops(true);
|
2014-02-12 11:42:01 +01:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
2011-12-29 15:21:26 +01:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
m_layout = new QHBoxLayout(this);
|
|
|
|
m_layout->setMargin(1);
|
|
|
|
m_layout->setSpacing(0);
|
|
|
|
setLayout(m_layout);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-02-12 11:42:01 +01:00
|
|
|
// Set some sane value, will be updated to correct value on first added button
|
|
|
|
setMinimumHeight(25);
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
m_updateTimer = new QTimer(this);
|
|
|
|
m_updateTimer->setInterval(300);
|
|
|
|
m_updateTimer->setSingleShot(true);
|
|
|
|
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(refresh()));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
connect(m_bookmarks, SIGNAL(bookmarkAdded(BookmarkItem*)), this, SLOT(bookmarksChanged()));
|
|
|
|
connect(m_bookmarks, SIGNAL(bookmarkRemoved(BookmarkItem*)), this, SLOT(bookmarksChanged()));
|
|
|
|
connect(m_bookmarks, SIGNAL(bookmarkChanged(BookmarkItem*)), this, SLOT(bookmarksChanged()));
|
2014-02-10 14:57:48 +01:00
|
|
|
connect(m_bookmarks, SIGNAL(showOnlyIconsInToolbarChanged(bool)), this, SLOT(showOnlyIconsChanged(bool)));
|
2014-02-10 11:54:58 +01:00
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
refresh();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::contextMenuRequested(const QPoint &pos)
|
2011-10-28 17:52:42 +02:00
|
|
|
{
|
2014-02-10 17:09:42 +01:00
|
|
|
BookmarksToolbarButton* button = buttonAt(pos);
|
2014-02-10 11:54:58 +01:00
|
|
|
m_clickedBookmark = button ? button->bookmark() : 0;
|
2011-10-28 17:52:42 +02:00
|
|
|
|
|
|
|
QMenu menu;
|
2014-02-10 11:54:58 +01:00
|
|
|
QAction* actNewTab = menu.addAction(QIcon::fromTheme("tab-new", QIcon(":/icons/menu/tab-new.png")), tr("Open in new tab"));
|
|
|
|
QAction* actNewWindow = menu.addAction(QIcon::fromTheme("window-new"), tr("Open in new window"));
|
2012-10-05 22:15:18 +02:00
|
|
|
menu.addSeparator();
|
2014-02-10 11:54:58 +01:00
|
|
|
QAction* actDelete = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
|
2011-10-28 17:52:42 +02:00
|
|
|
menu.addSeparator();
|
2014-02-10 11:54:58 +01:00
|
|
|
QAction* act = menu.addAction(tr("Show Only Icons"));
|
|
|
|
act->setCheckable(true);
|
2014-02-10 12:58:34 +01:00
|
|
|
act->setChecked(m_bookmarks->showOnlyIconsInToolbar());
|
|
|
|
connect(act, SIGNAL(toggled(bool)), m_bookmarks, SLOT(setShowOnlyIconsInToolbar(bool)));
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab()));
|
|
|
|
connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow()));
|
|
|
|
connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteBookmark()));
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-11 15:33:02 +01:00
|
|
|
actDelete->setEnabled(m_clickedBookmark && m_bookmarks->canBeModified(m_clickedBookmark));
|
2014-02-10 11:54:58 +01:00
|
|
|
actNewTab->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl());
|
|
|
|
actNewWindow->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl());
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-10 17:09:42 +01:00
|
|
|
menu.exec(mapToGlobal(pos));
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2014-02-11 10:24:44 +01:00
|
|
|
if (button) {
|
|
|
|
// Clear mouseover state after closing menu
|
|
|
|
button->update();
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
m_clickedBookmark = 0;
|
2011-10-28 17:52:42 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::refresh()
|
2011-10-18 21:07:58 +02:00
|
|
|
{
|
2014-02-10 11:54:58 +01:00
|
|
|
clear();
|
2012-10-05 22:15:18 +02:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
BookmarkItem* folder = mApp->bookmarks()->toolbarFolder();
|
2011-12-19 21:30:26 +01:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
foreach (BookmarkItem* child, folder->children()) {
|
|
|
|
addItem(child);
|
2011-12-19 21:30:26 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
m_layout->addStretch();
|
2011-12-19 21:30:26 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::bookmarksChanged()
|
2011-09-11 19:15:06 +02:00
|
|
|
{
|
2014-02-10 11:54:58 +01:00
|
|
|
m_updateTimer->start();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
void BookmarksToolbar::showOnlyIconsChanged(bool state)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_layout->count(); ++i) {
|
|
|
|
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
|
|
|
|
if (b) {
|
|
|
|
b->setShowOnlyIcon(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::openBookmarkInNewTab()
|
2011-10-28 17:52:42 +02:00
|
|
|
{
|
2014-02-10 11:54:58 +01:00
|
|
|
if (m_clickedBookmark) {
|
|
|
|
BookmarksTools::openBookmarkInNewTab(m_window, m_clickedBookmark);
|
2011-10-28 17:52:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::openBookmarkInNewWindow()
|
2011-04-15 20:45:22 +02:00
|
|
|
{
|
2014-02-10 11:54:58 +01:00
|
|
|
if (m_clickedBookmark) {
|
|
|
|
BookmarksTools::openBookmarkInNewWindow(m_clickedBookmark);
|
2011-10-28 17:52:42 +02:00
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
void BookmarksToolbar::deleteBookmark()
|
2011-10-28 17:52:42 +02:00
|
|
|
{
|
2014-02-10 11:54:58 +01:00
|
|
|
if (m_clickedBookmark) {
|
|
|
|
m_bookmarks->removeBookmark(m_clickedBookmark);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-10-28 17:52:42 +02:00
|
|
|
}
|
|
|
|
|
2014-02-10 17:09:42 +01:00
|
|
|
void BookmarksToolbar::clear()
|
|
|
|
{
|
|
|
|
int count = m_layout->count();
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
QLayoutItem* item = m_layout->takeAt(0);
|
|
|
|
delete item->widget();
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_ASSERT(m_layout->isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::addItem(BookmarkItem* item)
|
|
|
|
{
|
|
|
|
Q_ASSERT(item);
|
|
|
|
|
2014-02-11 15:33:02 +01:00
|
|
|
BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this);
|
|
|
|
button->setMainWindow(m_window);
|
|
|
|
button->setShowOnlyIcon(m_bookmarks->showOnlyIconsInToolbar());
|
|
|
|
m_layout->addWidget(button);
|
2014-02-12 11:42:01 +01:00
|
|
|
|
|
|
|
setFixedHeight(m_layout->spacing() * 2 + button->preferredHeight());
|
2014-02-10 17:09:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BookmarksToolbarButton* BookmarksToolbar::buttonAt(const QPoint &pos)
|
|
|
|
{
|
|
|
|
return qobject_cast<BookmarksToolbarButton*>(QApplication::widgetAt(mapToGlobal(pos)));
|
|
|
|
}
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
void BookmarksToolbar::dropEvent(QDropEvent* e)
|
|
|
|
{
|
|
|
|
const QMimeData* mime = e->mimeData();
|
|
|
|
|
2014-02-10 17:09:42 +01:00
|
|
|
if (!mime->hasUrls()) {
|
2012-03-05 11:30:18 +01:00
|
|
|
QWidget::dropEvent(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl url = mime->urls().at(0);
|
2014-02-10 17:09:42 +01:00
|
|
|
QString title = mime->hasText() ? mime->text() : url.toEncoded(QUrl::RemoveScheme);
|
|
|
|
|
|
|
|
BookmarkItem* parent = m_bookmarks->toolbarFolder();
|
|
|
|
BookmarksToolbarButton* button = buttonAt(e->pos());
|
|
|
|
if (button && button->bookmark()->isFolder()) {
|
|
|
|
parent = button->bookmark();
|
|
|
|
}
|
2012-03-05 11:30:18 +01:00
|
|
|
|
2014-02-10 11:54:58 +01:00
|
|
|
BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url);
|
|
|
|
bookmark->setTitle(title);
|
|
|
|
bookmark->setUrl(url);
|
2014-02-10 17:09:42 +01:00
|
|
|
m_bookmarks->addBookmark(parent, bookmark);
|
2012-03-05 11:30:18 +01:00
|
|
|
}
|
|
|
|
|
2011-12-17 16:04:04 +01:00
|
|
|
void BookmarksToolbar::dragEnterEvent(QDragEnterEvent* e)
|
2011-12-17 14:26:34 +01:00
|
|
|
{
|
|
|
|
const QMimeData* mime = e->mimeData();
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
if (mime->hasUrls() && mime->hasText()) {
|
2011-12-17 14:26:34 +01:00
|
|
|
e->acceptProposedAction();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-05 11:30:18 +01:00
|
|
|
QWidget::dragEnterEvent(e);
|
2011-12-17 14:26:34 +01:00
|
|
|
}
|