1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 09:32:12 +01:00

[Bookmarks] Bookmarks Toolbar is now working

This commit is contained in:
nowrep 2014-02-10 11:54:58 +01:00
parent 946354a15d
commit 6ab92b39bc
9 changed files with 619 additions and 528 deletions

View File

@ -77,7 +77,7 @@ void BookmarksMenu::aboutToShow()
}
}
void BookmarksMenu::loadMenu(Menu* menu)
void BookmarksMenu::menuMiddleClicked(Menu* menu)
{
BookmarkItem* item = static_cast<BookmarkItem*>(menu->menuAction()->data().value<void*>());
Q_ASSERT(item);
@ -155,10 +155,10 @@ void BookmarksMenu::init()
addSeparator();
connect(this, SIGNAL(aboutToShow()), this, SLOT(aboutToShow()));
connect(this, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(loadMenu(Menu*)));
connect(this, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(menuMiddleClicked(Menu*)));
}
#define FOLDER_ICON QApplication::style()->standardIcon(QStyle::SP_DirIcon)
#define FOLDER_ICON style()->standardIcon(QStyle::SP_DirIcon)
void BookmarksMenu::refresh()
{
@ -171,63 +171,13 @@ void BookmarksMenu::refresh()
delete act;
}
addItem(this, mApp->bookmarks()->toolbarFolder());
BookmarksTools::addActionToMenu(this, this, mApp->bookmarks()->toolbarFolder());
addSeparator();
foreach (BookmarkItem* child, mApp->bookmarks()->menuFolder()->children()) {
addItem(this, child);
BookmarksTools::addActionToMenu(this, this, child);
}
addSeparator();
addItem(this, mApp->bookmarks()->unsortedFolder());
}
void BookmarksMenu::addItem(Menu* menu, BookmarkItem* item)
{
Q_ASSERT(menu);
Q_ASSERT(item);
switch (item->type()) {
case BookmarkItem::Url:
addBookmark(menu, item);
break;
case BookmarkItem::Folder:
addFolder(menu, item);
break;
case BookmarkItem::Separator:
menu->addSeparator();
break;
default:
break;
}
}
void BookmarksMenu::addFolder(Menu* menu, BookmarkItem* folder)
{
Menu* m = new Menu(folder->title());
m->setIcon(FOLDER_ICON);
connect(m, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(loadMenu(Menu*)));
QAction* act = menu->addMenu(m);
act->setData(QVariant::fromValue<void*>(static_cast<void*>(folder)));
foreach (BookmarkItem* child, folder->children()) {
addItem(m, child);
}
if (folder->children().isEmpty()) {
m->addAction(tr("Empty"))->setDisabled(true);
}
}
void BookmarksMenu::addBookmark(Menu* menu, BookmarkItem* bookmark)
{
Action* act = new Action(_iconForUrl(bookmark->url()), bookmark->title());
act->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)));
connect(act, SIGNAL(triggered()), this, SLOT(bookmarkActivated()));
connect(act, SIGNAL(ctrlTriggered()), this, SLOT(bookmarkCtrlActivated()));
connect(act, SIGNAL(shiftTriggered()), this, SLOT(bookmarkShiftActivated()));
menu->addAction(act);
BookmarksTools::addActionToMenu(this, this, mApp->bookmarks()->unsortedFolder());
}

View File

@ -42,7 +42,7 @@ private slots:
void bookmarksChanged();
void aboutToShow();
void loadMenu(Menu* menu);
void menuMiddleClicked(Menu* menu);
void bookmarkActivated();
void bookmarkCtrlActivated();
@ -57,10 +57,6 @@ private:
void init();
void refresh();
void addItem(Menu* menu, BookmarkItem* item);
void addFolder(Menu* menu, BookmarkItem* folder);
void addBookmark(Menu* menu, BookmarkItem* bookmark);
QPointer<QupZilla> m_window;
bool m_changed;
};

View File

@ -16,445 +16,165 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarkstoolbar.h"
#include "qupzilla.h"
#include "bookmarkstoolbarbutton.h"
#include "bookmarkstools.h"
#include "bookmarkitem.h"
#include "mainapplication.h"
#include "bookmarks.h"
#include "iconprovider.h"
#include "history.h"
#include "toolbutton.h"
#include "databasewriter.h"
#include "enhancedmenu.h"
#include "tabwidget.h"
#include <QDialog>
#include <QIcon>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QLineEdit>
#include <QLabel>
#include <QDialogButtonBox>
#include <QMimeData>
#include <QDragEnterEvent>
#include <QHBoxLayout>
#include <QMimeData>
#include <QTimer>
#include <QFrame>
BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent)
: QWidget(parent)
, p_QupZilla(mainClass)
, m_window(mainClass)
, m_bookmarks(mApp->bookmarks())
, m_historyModel(mApp->history())
, m_toolButtonStyle(Qt::ToolButtonTextBesideIcon)
, m_clickedBookmark(0)
{
setObjectName("bookmarksbar");
m_layout = new QHBoxLayout();
m_layout->setMargin(3);
setContextMenuPolicy(Qt::CustomContextMenu);
setAcceptDrops(true);
m_layout = new QHBoxLayout(this);
m_layout->setMargin(1);
m_layout->setSpacing(0);
setLayout(m_layout);
setAcceptDrops(true);
m_updateTimer = new QTimer(this);
m_updateTimer->setInterval(300);
m_updateTimer->setSingleShot(true);
connect(m_updateTimer, SIGNAL(timeout()), this, SLOT(refresh()));
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
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()));
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequested(QPoint)));
setMaximumWidth(p_QupZilla->width());
refreshBookmarks();
showOnlyIconsChanged();
refresh();
}
void BookmarksToolbar::customContextMenuRequested(const QPoint &pos)
void BookmarksToolbar::contextMenuRequested(const QPoint &pos)
{
Q_UNUSED(pos)
QPoint globalPos = mapToGlobal(pos);
BookmarksToolbarButton* button = qobject_cast<BookmarksToolbarButton*>(QApplication::widgetAt(globalPos));
m_clickedBookmark = button ? button->bookmark() : 0;
QMenu menu;
menu.addAction(tr("&Bookmark Current Page"), p_QupZilla, SLOT(bookmarkPage()));
menu.addAction(tr("Bookmark &All Tabs"), p_QupZilla, SLOT(bookmarkAllTabs()));
menu.addAction(qIconProvider->fromTheme("bookmarks-organize"), tr("&Organize Bookmarks"), p_QupZilla, SLOT(showBookmarksManager()));
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"));
menu.addSeparator();
QAction act(tr("Show Most &Visited"), this);
act.setCheckable(true);
act.setChecked(m_bookmarks->isShowingMostVisited());
connect(&act, SIGNAL(triggered()), this, SLOT(showMostVisited()));
menu.addAction(&act);
QAction act2(tr("Show Only Icons"), this);
act2.setCheckable(true);
act2.setChecked(m_bookmarks->isShowingOnlyIconsInToolbar());
connect(&act2, SIGNAL(triggered()), this, SLOT(toggleShowOnlyIcons()));
menu.addAction(&act2);
QAction* actDelete = menu.addAction(QIcon::fromTheme("edit-delete"), tr("Delete"));
menu.addSeparator();
menu.addAction(tr("&Hide Toolbar"), this, SLOT(hidePanel()));
QAction* act = menu.addAction(tr("Show Only Icons"));
act->setCheckable(true);
act->setChecked(m_bookmarks->isShowingOnlyIconsInToolbar());
connect(act, SIGNAL(toggled(bool)), this, SLOT(setShowOnlyIcons(bool)));
//Prevent choosing first option with double rightclick
QPoint position = mapToGlobal(pos);
QPoint p(position.x(), position.y() + 1);
menu.exec(p);
connect(actNewTab, SIGNAL(triggered()), this, SLOT(openBookmarkInNewTab()));
connect(actNewWindow, SIGNAL(triggered()), this, SLOT(openBookmarkInNewWindow()));
connect(actDelete, SIGNAL(triggered()), this, SLOT(deleteBookmark()));
actDelete->setEnabled(m_clickedBookmark && (m_clickedBookmark->isUrl() || m_clickedBookmark->isFolder()));
actNewTab->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl());
actNewWindow->setEnabled(m_clickedBookmark && m_clickedBookmark->isUrl());
menu.exec(globalPos);
m_clickedBookmark = 0;
}
void BookmarksToolbar::showBookmarkContextMenu(const QPoint &pos)
void BookmarksToolbar::setShowOnlyIcons(bool show)
{
Q_UNUSED(pos)
m_bookmarks->setShowingOnlyIconsInToolbar(show);
ToolButton* button = qobject_cast<ToolButton*>(sender());
if (!button) {
return;
}
for (int i = 0; i < m_layout->count(); ++i) {
BookmarksToolbarButton* b = qobject_cast<BookmarksToolbarButton*>(m_layout->itemAt(i)->widget());
QVariant buttonPointer = QVariant::fromValue((void*) button);
QMenu menu;
menu.addAction(tr("Open bookmark"), this, SLOT(loadClickedBookmark()))->setData(buttonPointer);
menu.addAction(tr("Open bookmark in new tab"), this, SLOT(loadClickedBookmarkInNewTab()))->setData(buttonPointer);
menu.addSeparator();
// in RTL layouts we should reverse Move Right/Left
if (!isRightToLeft()) {
menu.addAction(qIconProvider->fromTheme("go-next"), tr("Move right"), this, SLOT(moveRight()))->setData(buttonPointer);
menu.addAction(qIconProvider->fromTheme("go-previous"), tr("Move left"), this, SLOT(moveLeft()))->setData(buttonPointer);
}
else {
menu.addAction(qIconProvider->fromTheme("go-next"), tr("Move right"), this, SLOT(moveLeft()))->setData(buttonPointer);
menu.addAction(qIconProvider->fromTheme("go-previous"), tr("Move left"), this, SLOT(moveRight()))->setData(buttonPointer);
}
menu.addAction(tr("Edit bookmark"), this, SLOT(editBookmark()))->setData(buttonPointer);
menu.addSeparator();
menu.addAction(qIconProvider->fromTheme("list-remove"), tr("Remove bookmark"), this, SLOT(removeButton()))->setData(buttonPointer);
//Prevent choosing first option with double rightclick
QPoint position = button->mapToGlobal(pos);
QPoint p(position.x(), position.y() + 1);
menu.exec(p);
}
void BookmarksToolbar::moveRight()
{
QAction* act = qobject_cast<QAction*> (sender());
if (!act) {
return;
}
ToolButton* button = static_cast<ToolButton*>(act->data().value<void*>());
int index = m_layout->indexOf(button);
if (index == m_layout->count() - 1) {
return;
}
ToolButton* buttonRight = qobject_cast<ToolButton*> (m_layout->itemAt(index + 1)->widget());
if (!buttonRight || buttonRight->menu()) {
return;
}
Bookmark bookmark = button->data().value<Bookmark>();
Bookmark bookmarkRight = buttonRight->data().value<Bookmark>();
QSqlQuery query;
query.prepare("UPDATE bookmarks SET position=? WHERE id=?");
query.addBindValue(index + 1);
query.addBindValue(bookmark.id);
mApp->dbWriter()->executeQuery(query);
query.prepare("UPDATE bookmarks SET position=? WHERE id=?");
query.addBindValue(index);
query.addBindValue(bookmarkRight.id);
mApp->dbWriter()->executeQuery(query);
QWidget* w = m_layout->takeAt(index)->widget();
m_layout->insertWidget(index + 1, w);
}
void BookmarksToolbar::moveLeft()
{
QAction* act = qobject_cast<QAction*> (sender());
if (!act) {
return;
}
ToolButton* button = static_cast<ToolButton*>(act->data().value<void*>());
int index = m_layout->indexOf(button);
if (index == 0) {
return;
}
ToolButton* buttonLeft = qobject_cast<ToolButton*> (m_layout->itemAt(index - 1)->widget());
if (!buttonLeft) {
return;
}
Bookmark bookmark = button->data().value<Bookmark>();
Bookmark bookmarkLeft = buttonLeft->data().value<Bookmark>();
QSqlQuery query;
query.prepare("UPDATE bookmarks SET position=? WHERE id=?");
query.addBindValue(index - 1);
query.addBindValue(bookmark.id);
mApp->dbWriter()->executeQuery(query);
query.prepare("UPDATE bookmarks SET position=? WHERE id=?");
query.addBindValue(index);
query.addBindValue(bookmarkLeft.id);
mApp->dbWriter()->executeQuery(query);
QWidget* w = m_layout->takeAt(index)->widget();
m_layout->insertWidget(index - 1, w);
}
void BookmarksToolbar::editBookmark()
{
QAction* act = qobject_cast<QAction*> (sender());
if (!act) {
return;
}
ToolButton* button = static_cast<ToolButton*>(act->data().value<void*>());
if (!button) {
return;
}
Bookmark b = button->data().value<Bookmark>();
QDialog* dialog = new QDialog(this);
QFormLayout* layout = new QFormLayout(dialog);
QLabel* label = new QLabel(dialog);
QLineEdit* editUrl = new QLineEdit(dialog);
QLineEdit* editTitle = new QLineEdit(dialog);
QDialogButtonBox* box = new QDialogButtonBox(dialog);
box->addButton(QDialogButtonBox::Ok);
box->addButton(QDialogButtonBox::Cancel);
connect(box, SIGNAL(rejected()), dialog, SLOT(reject()));
connect(box, SIGNAL(accepted()), dialog, SLOT(accept()));
label->setText(tr("Edit bookmark: "));
layout->addRow(label);
layout->addRow(new QLabel(tr("Title: ")), editTitle);
layout->addRow(new QLabel(tr("Url: ")), editUrl);
layout->addRow(box);
editUrl->setText(b.url.toString());
editTitle->setText(b.title);
editUrl->setCursorPosition(0);
editTitle->setCursorPosition(0);
dialog->setWindowTitle(tr("Edit Bookmark"));
dialog->setMinimumSize(400, 100);
dialog->exec();
if (dialog->result() == QDialog::Rejected) {
return;
}
QUrl url = QUrl::fromEncoded(editUrl->text().toUtf8());
QString title = editTitle->text();
if (url.isEmpty() || title.isEmpty()) {
return;
}
//m_bookmarks->editBookmark(b.id, title, url, b.folder);
}
void BookmarksToolbar::removeButton()
{
QAction* act = qobject_cast<QAction*> (sender());
if (!act) {
return;
}
ToolButton* button = static_cast<ToolButton*>(act->data().value<void*>());
if (!button) {
return;
}
Bookmark bookmark = button->data().value<Bookmark>();
//m_bookmarks->removeBookmark(bookmark.id);
}
void BookmarksToolbar::hidePanel()
{
p_QupZilla->showBookmarksToolbar();
}
void BookmarksToolbar::toggleShowOnlyIcons()
{
m_bookmarks->setShowingOnlyIconsInToolbar(!m_bookmarks->isShowingOnlyIconsInToolbar());
showOnlyIconsChanged();
}
void BookmarksToolbar::loadClickedBookmark()
{
ToolButton* button = 0;
QAction* act = qobject_cast<QAction*> (sender());
if (act) {
button = static_cast<ToolButton*>(act->data().value<void*>());
}
if (!button) {
button = qobject_cast<ToolButton*>(sender());
}
if (!button) {
return;
}
Bookmark bookmark = button->data().value<Bookmark>();
p_QupZilla->loadAddress(bookmark.url);
}
void BookmarksToolbar::loadClickedBookmarkInNewTab()
{
ToolButton* button = 0;
QAction* act = qobject_cast<QAction*> (sender());
if (act) {
button = static_cast<ToolButton*>(act->data().value<void*>());
}
if (!button) {
button = qobject_cast<ToolButton*>(sender());
}
if (!button) {
return;
}
Bookmark bookmark = button->data().value<Bookmark>();
p_QupZilla->tabWidget()->addView(bookmark.url, bookmark.title);
}
void BookmarksToolbar::loadFolderBookmarksInTabs()
{
ToolButton* b = qobject_cast<ToolButton*>(sender());
if (!b) {
return;
}
QString folder = b->text();
if (folder.isEmpty()) {
return;
}
//foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
// p_QupZilla->tabWidget()->addView(b.url, b.title, Qz::NT_NotSelectedTab);
//}
}
void BookmarksToolbar::showMostVisited()
{
m_bookmarks->setShowingMostVisited(!m_bookmarks->isShowingMostVisited());
m_mostVis->setVisible(!m_mostVis->isVisible());
}
int BookmarksToolbar::indexOfLastBookmark()
{
for (int i = m_layout->count() - 1; i >= 0; i--) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
if (!button->menu()) {
return i + 1;
if (b) {
b->setShowOnlyIcon(show);
}
}
return 0;
}
void BookmarksToolbar::refreshBookmarks()
void BookmarksToolbar::addItem(BookmarkItem* item)
{
QSqlQuery query;
query.exec("SELECT id, title, url, icon FROM bookmarks WHERE folder='bookmarksToolbar' ORDER BY position");
while (query.next()) {
Bookmark bookmark;
bookmark.id = query.value(0).toInt();
bookmark.title = query.value(1).toString();
bookmark.url = query.value(2).toUrl();
bookmark.image = QImage::fromData(query.value(3).toByteArray());
bookmark.folder = "bookmarksToolbar";
QString title = bookmark.title;
if (title.length() > 15) {
title.truncate(13);
title += "..";
}
Q_ASSERT(item);
QVariant v;
v.setValue<Bookmark>(bookmark);
ToolButton* button = new ToolButton(this);
button->setText(title);
button->setData(v);
button->setIcon(qIconProvider->iconFromImage(bookmark.image));
button->setToolButtonStyle(m_toolButtonStyle);
button->setToolTip(bookmark.url.toEncoded());
button->setAutoRaise(true);
button->setContextMenuPolicy(Qt::CustomContextMenu);
connect(button, SIGNAL(clicked()), this, SLOT(loadClickedBookmark()));
connect(button, SIGNAL(middleMouseClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
connect(button, SIGNAL(controlClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
connect(button, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showBookmarkContextMenu(QPoint)));
switch (item->type()) {
case BookmarkItem::Folder:
case BookmarkItem::Url: {
BookmarksToolbarButton* button = new BookmarksToolbarButton(item, this);
button->setMainWindow(m_window);
button->setShowOnlyIcon(m_bookmarks->isShowingOnlyIconsInToolbar());
m_layout->addWidget(button);
break;
}
query.exec("SELECT name FROM folders WHERE subfolder='yes'");
while (query.next()) {
ToolButton* b = new ToolButton(this);
b->setPopupMode(QToolButton::InstantPopup);
b->setToolButtonStyle(m_toolButtonStyle);
b->setIcon(style()->standardIcon(QStyle::SP_DirIcon));
b->setText(query.value(0).toString());
connect(b, SIGNAL(middleMouseClicked()), this, SLOT(loadFolderBookmarksInTabs()));
Menu* menu = new Menu(query.value(0).toString());
b->setMenu(menu);
connect(menu, SIGNAL(aboutToShow()), this, SLOT(aboutToShowFolderMenu()));
m_layout->addWidget(b);
case BookmarkItem::Separator: {
QFrame* separator = new QFrame(this);
separator->setFrameShape(QFrame::VLine);
m_layout->addWidget(separator);
break;
}
m_mostVis = new ToolButton(this);
m_mostVis->setPopupMode(QToolButton::InstantPopup);
m_mostVis->setToolButtonStyle(m_toolButtonStyle);
m_mostVis->setIcon(style()->standardIcon(QStyle::SP_DirIcon));
m_mostVis->setText(tr("Most visited"));
m_mostVis->setToolTip(tr("Sites you visited the most"));
m_menuMostVisited = new Menu();
m_mostVis->setMenu(m_menuMostVisited);
connect(m_menuMostVisited, SIGNAL(aboutToShow()), this, SLOT(refreshMostVisited()));
m_layout->addWidget(m_mostVis);
m_layout->addStretch();
m_mostVis->setVisible(m_bookmarks->isShowingMostVisited());
default:
break;
}
}
void BookmarksToolbar::aboutToShowFolderMenu()
void BookmarksToolbar::clear()
{
QMenu* menu = qobject_cast<QMenu*> (sender());
if (!menu) {
return;
int count = m_layout->count();
for (int i = 0; i < count; ++i) {
QLayoutItem* item = m_layout->takeAt(0);
delete item->widget();
delete item;
}
menu->clear();
//QString folder = menu->title();
Q_ASSERT(m_layout->isEmpty());
}
//foreach (const Bookmark &b, m_bookmarks->getFolderBookmarks(folder)) {
// QString title = b.title;
// if (title.length() > 40) {
// title.truncate(40);
// title += "..";
// }
void BookmarksToolbar::refresh()
{
clear();
// Action* act = new Action(qIconProvider->iconFromImage(b.image), title);
// act->setData(b.url);
// connect(act, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
// connect(act, SIGNAL(ctrlTriggered()), p_QupZilla, SLOT(loadActionUrlInNewNotSelectedTab()));
// menu->addAction(act);
//}
BookmarkItem* folder = mApp->bookmarks()->toolbarFolder();
if (menu->isEmpty()) {
menu->addAction(tr("Empty"))->setEnabled(false);
foreach (BookmarkItem* child, folder->children()) {
addItem(child);
}
m_layout->addStretch();
}
void BookmarksToolbar::bookmarksChanged()
{
m_updateTimer->stop();
m_updateTimer->start();
}
void BookmarksToolbar::openBookmarkInNewTab()
{
if (m_clickedBookmark) {
BookmarksTools::openBookmarkInNewTab(m_window, m_clickedBookmark);
}
}
void BookmarksToolbar::openBookmarkInNewWindow()
{
if (m_clickedBookmark) {
BookmarksTools::openBookmarkInNewWindow(m_clickedBookmark);
}
}
void BookmarksToolbar::deleteBookmark()
{
if (m_clickedBookmark) {
m_bookmarks->removeBookmark(m_clickedBookmark);
}
}
@ -469,9 +189,11 @@ void BookmarksToolbar::dropEvent(QDropEvent* e)
QString title = mime->text();
QUrl url = mime->urls().at(0);
QIcon icon = qIconProvider->iconFromImage(qvariant_cast<QImage>(mime->imageData()));
//m_bookmarks->saveBookmark(url, title, icon, "bookmarksToolbar");
BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url);
bookmark->setTitle(title);
bookmark->setUrl(url);
m_bookmarks->addBookmark(m_bookmarks->toolbarFolder(), bookmark);
}
void BookmarksToolbar::dragEnterEvent(QDragEnterEvent* e)
@ -485,41 +207,3 @@ void BookmarksToolbar::dragEnterEvent(QDragEnterEvent* e)
QWidget::dragEnterEvent(e);
}
void BookmarksToolbar::showOnlyIconsChanged()
{
m_toolButtonStyle = m_bookmarks->isShowingOnlyIconsInToolbar() ? Qt::ToolButtonIconOnly : Qt::ToolButtonTextBesideIcon;
for (int i = 0; i < m_layout->count(); ++i) {
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
if (!button) {
continue;
}
button->setToolButtonStyle(m_toolButtonStyle);
}
}
void BookmarksToolbar::refreshMostVisited()
{
m_menuMostVisited->clear();
QVector<HistoryEntry> mostList = m_historyModel->mostVisited(10);
foreach (const HistoryEntry &entry, mostList) {
QString title = entry.title;
if (title.length() > 40) {
title.truncate(40);
title += "..";
}
Action* act = new Action(_iconForUrl(entry.url), title);
act->setData(entry.url);
connect(act, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
connect(act, SIGNAL(ctrlTriggered()), p_QupZilla, SLOT(loadActionUrlInNewNotSelectedTab()));
m_menuMostVisited->addAction(act);
}
if (m_menuMostVisited->isEmpty()) {
m_menuMostVisited->addAction(tr("Empty"))->setEnabled(false);
}
}

View File

@ -21,15 +21,13 @@
#include <QWidget>
#include "qz_namespace.h"
#include "bookmarks.h"
class QHBoxLayout;
class QTimer;
class QupZilla;
class Bookmarks;
class History;
class ToolButton;
class Menu;
class BookmarkItem;
class QT_QUPZILLA_EXPORT BookmarksToolbar : public QWidget
{
@ -37,45 +35,29 @@ class QT_QUPZILLA_EXPORT BookmarksToolbar : public QWidget
public:
explicit BookmarksToolbar(QupZilla* mainClass, QWidget* parent = 0);
signals:
public slots:
void refreshBookmarks();
void refreshMostVisited();
void showMostVisited();
private slots:
void loadClickedBookmark();
void loadClickedBookmarkInNewTab();
void loadFolderBookmarksInTabs();
void contextMenuRequested(const QPoint &pos);
void setShowOnlyIcons(bool show);
void aboutToShowFolderMenu();
void showBookmarkContextMenu(const QPoint &pos);
void customContextMenuRequested(const QPoint &pos);
void refresh();
void bookmarksChanged();
void moveRight();
void moveLeft();
void editBookmark();
void removeButton();
void hidePanel();
void toggleShowOnlyIcons();
void openBookmarkInNewTab();
void openBookmarkInNewWindow();
void deleteBookmark();
private:
void clear();
void addItem(BookmarkItem* item);
void dropEvent(QDropEvent* e);
void dragEnterEvent(QDragEnterEvent* e);
void showOnlyIconsChanged();
int indexOfLastBookmark();
QupZilla* p_QupZilla;
QupZilla* m_window;
Bookmarks* m_bookmarks;
History* m_historyModel;
Menu* m_menuMostVisited;
ToolButton* m_mostVis;
BookmarkItem* m_clickedBookmark;
QHBoxLayout* m_layout;
Qt::ToolButtonStyle m_toolButtonStyle;
QTimer* m_updateTimer;
};
#endif // BOOKMARKSTOOLBAR_H

View File

@ -0,0 +1,320 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#include "bookmarkstoolbarbutton.h"
#include "bookmarkstools.h"
#include "bookmarkitem.h"
#include "bookmarks.h"
#include "mainapplication.h"
#include "iconprovider.h"
#include "enhancedmenu.h"
#include <QStyle>
#include <QPainter>
#include <QMouseEvent>
#include <QStyleOptionButton>
BookmarksToolbarButton::BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* parent)
: QPushButton(parent)
, m_bookmark(bookmark)
, m_window(0)
, m_buttons(Qt::NoButton)
, m_modifiers(Qt::NoModifier)
, m_showOnlyIcon(false)
, m_padding(5)
{
init();
}
BookmarkItem* BookmarksToolbarButton::bookmark() const
{
return m_bookmark;
}
void BookmarksToolbarButton::setMainWindow(QupZilla* window)
{
m_window = window;
}
bool BookmarksToolbarButton::showOnlyIcon() const
{
return m_showOnlyIcon;
}
void BookmarksToolbarButton::setShowOnlyIcon(bool show)
{
m_showOnlyIcon = show;
setText(show ? QString() : m_bookmark->title());
}
QSize BookmarksToolbarButton::sizeHint() const
{
const int maxWidth = 150;
int width = m_padding * 2 + 16;
if (!m_showOnlyIcon) {
width += m_padding * 2 + fontMetrics().width(text());
if (menu()) {
width += m_padding + 8;
}
}
QSize s = QPushButton::sizeHint();
s.setWidth(qMin(width, maxWidth));
return s;
}
QSize BookmarksToolbarButton::minimumSizeHint() const
{
int width = m_padding * 2 + 16;
if (!m_showOnlyIcon && menu()) {
width += m_padding + 8;
}
QSize s = QPushButton::minimumSizeHint();
s.setWidth(width);
return s;
}
void BookmarksToolbarButton::createMenu()
{
if (!menu()->isEmpty()) {
return;
}
Menu* m = qobject_cast<Menu*>(menu());
Q_ASSERT(m);
foreach (BookmarkItem* child, m_bookmark->children()) {
BookmarksTools::addActionToMenu(this, m, child);
}
if (m->isEmpty()) {
m->addAction(Bookmarks::tr("Empty"))->setDisabled(true);
}
}
void BookmarksToolbarButton::menuMiddleClicked(Menu* menu)
{
BookmarkItem* item = static_cast<BookmarkItem*>(menu->menuAction()->data().value<void*>());
Q_ASSERT(item);
openFolder(item);
}
void BookmarksToolbarButton::bookmarkActivated(BookmarkItem* item)
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
item = static_cast<BookmarkItem*>(action->data().value<void*>());
}
Q_ASSERT(item);
openBookmark(item);
}
void BookmarksToolbarButton::bookmarkCtrlActivated(BookmarkItem* item)
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
item = static_cast<BookmarkItem*>(action->data().value<void*>());
}
Q_ASSERT(item);
openBookmarkInNewTab(item);
}
void BookmarksToolbarButton::bookmarkShiftActivated(BookmarkItem* item)
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
item = static_cast<BookmarkItem*>(action->data().value<void*>());
}
Q_ASSERT(item);
openBookmarkInNewWindow(item);
}
void BookmarksToolbarButton::openFolder(BookmarkItem* item)
{
Q_ASSERT(item->isFolder());
if (m_window) {
BookmarksTools::openFolderInTabs(m_window, item);
}
}
void BookmarksToolbarButton::openBookmark(BookmarkItem* item)
{
Q_ASSERT(item->isUrl());
if (m_window) {
BookmarksTools::openBookmark(m_window, item);
}
}
void BookmarksToolbarButton::openBookmarkInNewTab(BookmarkItem* item)
{
Q_ASSERT(item->isUrl());
if (m_window) {
BookmarksTools::openBookmarkInNewTab(m_window, item);
}
}
void BookmarksToolbarButton::openBookmarkInNewWindow(BookmarkItem* item)
{
Q_ASSERT(item->isUrl());
BookmarksTools::openBookmarkInNewWindow(item);
}
void BookmarksToolbarButton::init()
{
Q_ASSERT(m_bookmark);
Q_ASSERT(m_bookmark->type() == BookmarkItem::Url || m_bookmark->type() == BookmarkItem::Folder);
setFlat(true);
setFocusPolicy(Qt::NoFocus);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
setIconSize(QSize(16, 16));
setMaximumHeight(24);
setText(createText());
setToolTip(createTooltip());
if (m_bookmark->isUrl()) {
setIcon(_iconForUrl(m_bookmark->url()));
}
else if (m_bookmark->isFolder()) {
setIcon(style()->standardIcon(QStyle::SP_DirIcon));
Menu* m = new Menu(this);
connect(m, SIGNAL(aboutToShow()), this, SLOT(createMenu()));
connect(m, SIGNAL(menuMiddleClicked(Menu*)), this, SLOT(menuMiddleClicked(Menu*)));
setMenu(m);
}
}
QString BookmarksToolbarButton::createTooltip() const
{
if (!m_bookmark->description().isEmpty()) {
return m_bookmark->description();
}
if (!m_bookmark->url().isEmpty()) {
return QString("%1\n%2").arg(m_bookmark->title(), m_bookmark->urlString());
}
return m_bookmark->title();
}
QString BookmarksToolbarButton::createText() const
{
return m_bookmark->title();
}
void BookmarksToolbarButton::mousePressEvent(QMouseEvent* event)
{
m_buttons = event->buttons();
m_modifiers = event->modifiers();
if (m_bookmark && m_bookmark->isFolder()) {
if (event->buttons() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier) {
openFolder(m_bookmark);
return;
}
}
QPushButton::mousePressEvent(event);
}
void BookmarksToolbarButton::mouseReleaseEvent(QMouseEvent* event)
{
if (m_bookmark && rect().contains(event->pos())) {
if (m_bookmark->isUrl()) {
if (m_buttons == Qt::LeftButton && m_modifiers == Qt::NoModifier) {
bookmarkActivated(m_bookmark);
}
else if (m_buttons == Qt::LeftButton && m_modifiers == Qt::ShiftModifier) {
bookmarkShiftActivated(m_bookmark);
}
else if (m_buttons == Qt::MiddleButton || m_modifiers == Qt::ControlModifier) {
bookmarkCtrlActivated(m_bookmark);
}
}
else if (m_bookmark->isFolder() && m_buttons == Qt::MiddleButton) {
openFolder(m_bookmark);
}
}
m_buttons = Qt::NoButton;
m_modifiers = Qt::NoModifier;
QPushButton::mouseReleaseEvent(event);
}
void BookmarksToolbarButton::paintEvent(QPaintEvent* event)
{
Q_UNUSED(event)
QPainter p(this);
QStyleOptionButton opt;
initStyleOption(&opt);
// This ensures correct rendering of Down state with Oxygen
if (isDown()) {
opt.state &= ~QStyle::State_MouseOver;
}
// Draw button base
style()->drawControl(QStyle::CE_PushButtonBevel, &opt, &p, this);
const int height = opt.rect.height();
const int center = height / 2 + opt.rect.top();
const int iconSize = 16;
const int iconYPos = center - iconSize / 2;
int leftPosition = m_padding;
int rightPosition = opt.rect.right() - m_padding;
// Draw icon
QRect iconRect(leftPosition, iconYPos, iconSize, iconSize);
p.drawPixmap(iconRect, icon().pixmap(iconSize));
leftPosition = iconRect.right() + m_padding;
// Draw menu arrow
if (!m_showOnlyIcon && menu()) {
const int arrowSize = 8;
QStyleOption opt;
opt.initFrom(this);
opt.rect = QRect(rightPosition - 8, center - arrowSize / 2, arrowSize, arrowSize);
opt.state &= QStyle::State_MouseOver;
style()->drawPrimitive(QStyle::PE_IndicatorArrowDown, &opt, &p, this);
rightPosition = opt.rect.left() - m_padding;
}
// Draw text
if (!m_showOnlyIcon) {
const int textWidth = rightPosition - leftPosition;
const int textYPos = center - fontMetrics().height() / 2;
const QString txt = fontMetrics().elidedText(text(), Qt::ElideRight, textWidth);
QRect textRect(leftPosition, textYPos, textWidth, fontMetrics().height());
style()->drawItemText(&p, textRect, Qt::TextSingleLine | Qt::AlignCenter, opt.palette, true, txt);
}
}

View File

@ -0,0 +1,78 @@
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2014 David Rosca <nowrep@gmail.com>
*
* 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/>.
* ============================================================ */
#ifndef BOOKMARKSTOOLBARBUTTON_H
#define BOOKMARKSTOOLBARBUTTON_H
#include <QPushButton>
#include "qz_namespace.h"
class Menu;
class QupZilla;
class BookmarkItem;
class QT_QUPZILLA_EXPORT BookmarksToolbarButton : public QPushButton
{
Q_OBJECT
public:
explicit BookmarksToolbarButton(BookmarkItem* bookmark, QWidget* parent = 0);
BookmarkItem* bookmark() const;
void setMainWindow(QupZilla* window);
bool showOnlyIcon() const;
void setShowOnlyIcon(bool show);
QSize sizeHint() const;
QSize minimumSizeHint() const;
private slots:
void bookmarkRemoved(BookmarkItem* bookmark);
void createMenu();
void menuMiddleClicked(Menu* menu);
void bookmarkActivated(BookmarkItem* item = 0);
void bookmarkCtrlActivated(BookmarkItem* item = 0);
void bookmarkShiftActivated(BookmarkItem* item = 0);
void openFolder(BookmarkItem* item);
void openBookmark(BookmarkItem* item);
void openBookmarkInNewTab(BookmarkItem* item);
void openBookmarkInNewWindow(BookmarkItem* item);
private:
void init();
QString createTooltip() const;
QString createText() const;
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
void paintEvent(QPaintEvent* event);
BookmarkItem* m_bookmark;
QupZilla* m_window;
Qt::MouseButtons m_buttons;
Qt::KeyboardModifiers m_modifiers;
bool m_showOnlyIcon;
int m_padding;
};
#endif // BOOKMARKSTOOLBARBUTTON_H

View File

@ -20,6 +20,7 @@
#include "bookmarks.h"
#include "mainapplication.h"
#include "iconprovider.h"
#include "enhancedmenu.h"
#include "tabwidget.h"
#include "qzsettings.h"
#include "qupzilla.h"
@ -273,3 +274,71 @@ void BookmarksTools::openFolderInTabs(QupZilla* window, BookmarkItem* folder)
}
}
}
#define FOLDER_ICON QApplication::style()->standardIcon(QStyle::SP_DirIcon)
void BookmarksTools::addActionToMenu(QObject* receiver, Menu* menu, BookmarkItem* item)
{
Q_ASSERT(menu);
Q_ASSERT(item);
switch (item->type()) {
case BookmarkItem::Url:
addUrlToMenu(receiver, menu, item);
break;
case BookmarkItem::Folder:
addFolderToMenu(receiver, menu, item);
break;
case BookmarkItem::Separator:
addSeparatorToMenu(menu, item);
break;
default:
break;
}
}
void BookmarksTools::addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder)
{
Q_ASSERT(menu);
Q_ASSERT(folder);
Q_ASSERT(folder->isFolder());
Menu* m = new Menu(folder->title());
m->setIcon(FOLDER_ICON);
QObject::connect(m, SIGNAL(menuMiddleClicked(Menu*)), receiver, SLOT(menuMiddleClicked(Menu*)));
QAction* act = menu->addMenu(m);
act->setData(QVariant::fromValue<void*>(static_cast<void*>(folder)));
foreach (BookmarkItem* child, folder->children()) {
addActionToMenu(receiver, m, child);
}
if (m->isEmpty()) {
m->addAction(Bookmarks::tr("Empty"))->setDisabled(true);
}
}
void BookmarksTools::addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark)
{
Q_ASSERT(menu);
Q_ASSERT(bookmark);
Q_ASSERT(bookmark->isUrl());
Action* act = new Action(_iconForUrl(bookmark->url()), bookmark->title());
act->setData(QVariant::fromValue<void*>(static_cast<void*>(bookmark)));
QObject::connect(act, SIGNAL(triggered()), receiver, SLOT(bookmarkActivated()));
QObject::connect(act, SIGNAL(ctrlTriggered()), receiver, SLOT(bookmarkCtrlActivated()));
QObject::connect(act, SIGNAL(shiftTriggered()), receiver, SLOT(bookmarkShiftActivated()));
menu->addAction(act);
}
void BookmarksTools::addSeparatorToMenu(Menu* menu, BookmarkItem* separator)
{
Q_ASSERT(menu);
Q_ASSERT(separator->isSeparator());
menu->addSeparator();
}

View File

@ -71,17 +71,27 @@ private:
class QupZilla;
class TabWidget;
class Action;
class Menu;
class QT_QUPZILLA_EXPORT BookmarksTools
{
public:
// Add Bookmark Dialogs
static bool addBookmarkDialog(QWidget* parent, const QUrl &url, const QString &title, BookmarkItem* folder = 0);
static bool bookmarkAllTabsDialog(QWidget* parent, TabWidget* tabWidget, BookmarkItem* folder = 0);
// Open Bookmarks
static void openBookmark(QupZilla* window, BookmarkItem* item);
static void openBookmarkInNewTab(QupZilla* window, BookmarkItem* item);
static void openBookmarkInNewWindow(BookmarkItem* item);
static void openFolderInTabs(QupZilla* window, BookmarkItem* folder);
// Create Menu
static void addActionToMenu(QObject* receiver, Menu* menu, BookmarkItem* item);
static void addFolderToMenu(QObject* receiver, Menu* menu, BookmarkItem* folder);
static void addUrlToMenu(QObject* receiver, Menu* menu, BookmarkItem* bookmark);
static void addSeparatorToMenu(Menu* menu, BookmarkItem* separator);
};
#endif // BOOKMARKSTOOLS_H

View File

@ -258,7 +258,8 @@ SOURCES += \
bookmarks/bookmarkstools.cpp \
bookmarks/bookmarksmenu.cpp \
bookmarks/bookmarksicon.cpp \
bookmarks/bookmarksitemdelegate.cpp
bookmarks/bookmarksitemdelegate.cpp \
bookmarks/bookmarkstoolbarbutton.cpp
HEADERS += \
@ -452,7 +453,8 @@ HEADERS += \
bookmarks/bookmarkstools.h \
bookmarks/bookmarksmenu.h \
bookmarks/bookmarksicon.h \
bookmarks/bookmarksitemdelegate.h
bookmarks/bookmarksitemdelegate.h \
bookmarks/bookmarkstoolbarbutton.h
FORMS += \
preferences/autofillmanager.ui \