2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
2017-01-14 17:54:08 +01:00
|
|
|
* QupZilla - Qt web browser
|
|
|
|
* Copyright (C) 2010-2017 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"
|
2014-02-05 15:26:51 +01:00
|
|
|
#include "bookmarks.h"
|
2014-03-24 16:08:33 +01:00
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "iconprovider.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);
|
2016-04-05 09:29:48 +02:00
|
|
|
#ifndef Q_OS_MAC
|
|
|
|
m_layout->setSpacing(0);
|
|
|
|
#endif
|
2014-02-10 11:54:58 +01:00
|
|
|
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)));
|
2015-12-11 01:49:32 +01:00
|
|
|
connect(m_bookmarks, SIGNAL(showOnlyTextInToolbarChanged(bool)), this, SLOT(showOnlyTextChanged(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-03-24 16:08:33 +01:00
|
|
|
QAction* actNewTab = menu.addAction(IconProvider::newTabIcon(), tr("Open in new tab"));
|
|
|
|
QAction* actNewWindow = menu.addAction(IconProvider::newWindowIcon(), tr("Open in new window"));
|
2016-02-25 21:14:53 +01:00
|
|
|
QAction* actNewPrivateWindow = menu.addAction(IconProvider::privateBrowsingIcon(), tr("Open in new private window"));
|
2012-10-05 22:15:18 +02:00
|
|
|
menu.addSeparator();
|
2016-02-09 14:16:50 +01:00
|
|
|
QAction* actEdit = menu.addAction(tr("Edit"));
|
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();
|
2015-12-11 01:49:32 +01:00
|
|
|
m_actShowOnlyIcons = menu.addAction(tr("Show Only Icons"));
|
|
|
|
m_actShowOnlyIcons->setCheckable(true);
|
|
|
|
m_actShowOnlyIcons->setChecked(m_bookmarks->showOnlyIconsInToolbar());
|
|
|
|
connect(m_actShowOnlyIcons, SIGNAL(toggled(bool)), m_bookmarks, SLOT(setShowOnlyIconsInToolbar(bool)));
|
|
|
|
m_actShowOnlyText = menu.addAction(tr("Show Only Text"));
|
|
|
|
m_actShowOnlyText->setCheckable(true);
|
|
|
|
m_actShowOnlyText->setChecked(m_bookmarks->showOnlyTextInToolbar());
|
|
|
|
connect(m_actShowOnlyText, SIGNAL(toggled(bool)), m_bookmarks, SLOT(setShowOnlyTextInToolbar(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()));
|
2016-02-25 21:14:53 +01:00
|
|
|
connect(actNewPrivateWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewPrivateWindow()));
|
2016-02-09 14:16:50 +01:00
|
|
|
connect(actEdit, SIGNAL(triggered()), this, SLOT(editBookmark()));
|
2014-02-10 11:54:58 +01:00
|
|
|
connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteBookmark()));
|
2011-10-28 17:52:42 +02:00
|
|
|
|
2016-02-09 14:16:50 +01:00
|
|
|
actEdit->setEnabled(m_clickedBookmark && m_bookmarks->canBeModified(m_clickedBookmark));
|
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());
|
2016-02-25 21:14:53 +01:00
|
|
|
actNewPrivateWindow->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();
|
|
|
|
}
|
|
|
|
|
2017-01-14 17:54:08 +01:00
|
|
|
m_clickedBookmark = nullptr;
|
|
|
|
m_actShowOnlyIcons = nullptr;
|
|
|
|
m_actShowOnlyText = nullptr;
|
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)
|
|
|
|
{
|
2015-12-11 01:49:32 +01:00
|
|
|
if (state && m_actShowOnlyText) {
|
|
|
|
m_actShowOnlyText->setChecked(false);
|
|
|
|
}
|
|
|
|
|
2014-02-10 12:58:34 +01:00
|
|
|
for (int i = 0; i < m_layout->count(); ++i) {
|
|
|
|
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
|
|
|
|
if (b) {
|
|
|
|
b->setShowOnlyIcon(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 01:49:32 +01:00
|
|
|
void BookmarksToolbar::showOnlyTextChanged(bool state)
|
|
|
|
{
|
|
|
|
if (state && m_actShowOnlyIcons) {
|
|
|
|
m_actShowOnlyIcons->setChecked(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < m_layout->count(); ++i) {
|
|
|
|
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
|
|
|
|
if (b) {
|
|
|
|
b->setShowOnlyText(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
|
|
|
}
|
|
|
|
|
2016-02-25 21:14:53 +01:00
|
|
|
void BookmarksToolbar::openBookmarkInNewPrivateWindow()
|
|
|
|
{
|
|
|
|
if (m_clickedBookmark) {
|
|
|
|
BookmarksTools::openBookmarkInNewPrivateWindow(m_clickedBookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 14:16:50 +01:00
|
|
|
void BookmarksToolbar::editBookmark()
|
|
|
|
{
|
|
|
|
if (m_clickedBookmark) {
|
|
|
|
BookmarksTools::editBookmarkDialog(this, m_clickedBookmark);
|
|
|
|
m_bookmarks->changeBookmark(m_clickedBookmark);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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());
|
2016-02-18 19:32:32 +01:00
|
|
|
button->setShowOnlyText(m_bookmarks->showOnlyTextInToolbar());
|
2014-02-11 15:33:02 +01:00
|
|
|
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
|
|
|
}
|