2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
|
|
|
* Copyright (C) 2010-2011 nowrep
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "bookmarksmodel.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-03-17 17:03:04 +01:00
|
|
|
BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent) :
|
2011-03-02 16:57:41 +01:00
|
|
|
QToolBar(parent)
|
|
|
|
,p_QupZilla(mainClass)
|
|
|
|
,m_bookmarksModel(0)
|
|
|
|
{
|
|
|
|
setObjectName("bookmarksToolbar");
|
|
|
|
setWindowTitle(tr("Bookmarks"));
|
|
|
|
setStyleSheet("QToolBar{background-image:url(:icons/transp.png); border:none;}");
|
|
|
|
setMovable(false);
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
m_bookmarksModel = mApp->bookmarksModel();
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
|
2011-04-15 20:45:22 +02:00
|
|
|
|
|
|
|
connect(m_bookmarksModel, SIGNAL(bookmarkAdded(BookmarksModel::Bookmark)), this, SLOT(addBookmark(BookmarksModel::Bookmark)));
|
|
|
|
connect(m_bookmarksModel, SIGNAL(bookmarkDeleted(BookmarksModel::Bookmark)), this, SLOT(removeBookmark(BookmarksModel::Bookmark)));
|
|
|
|
connect(m_bookmarksModel, SIGNAL(bookmarkEdited(BookmarksModel::Bookmark,BookmarksModel::Bookmark)), this, SLOT(bookmarkEdited(BookmarksModel::Bookmark,BookmarksModel::Bookmark)));
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
QTimer::singleShot(0, this, SLOT(refreshBookmarks()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::setColor(QColor color)
|
|
|
|
{
|
|
|
|
setStyleSheet("QToolButton {color: "+color.name()+";}");
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::customContextMenuRequested(const QPoint &pos)
|
|
|
|
{
|
|
|
|
if (actionAt(pos))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QMenu menu;
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(tr("&Bookmark Current Page"), p_QupZilla, SLOT(bookmarkPage()));
|
|
|
|
menu.addAction(tr("Bookmark &All Tabs"), p_QupZilla, SLOT(bookmarkAllTabs()));
|
|
|
|
menu.addAction(QIcon::fromTheme("user-bookmarks"), tr("&Organize Bookmarks"), p_QupZilla, SLOT(showBookmarksManager()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
|
|
|
menu.addAction(
|
|
|
|
#ifdef Q_WS_X11
|
|
|
|
style()->standardIcon(QStyle::SP_BrowserReload)
|
|
|
|
#else
|
|
|
|
QIcon(":/icons/faenza/reload.png")
|
|
|
|
#endif
|
2011-04-09 00:22:50 +02:00
|
|
|
,tr("&Reload Toolbar"), this, SLOT(refreshBookmarks()));
|
2011-03-02 16:57:41 +01:00
|
|
|
menu.addSeparator();
|
2011-04-09 00:22:50 +02:00
|
|
|
menu.addAction(m_bookmarksModel->isShowingMostVisited() ? tr("Hide Most &Visited") : tr("Show Most &Visited"), this, SLOT(showMostVisited()));
|
|
|
|
menu.addAction(tr("&Hide Toolbar"), this, SLOT(hidePanel()));
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
//Prevent choosing first option with double rightclick
|
|
|
|
QPoint position = QCursor::pos();
|
|
|
|
QPoint p(position.x(), position.y()+1);
|
|
|
|
menu.exec(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::showMostVisited()
|
|
|
|
{
|
|
|
|
m_bookmarksModel->setShowingMostVisited(!m_bookmarksModel->isShowingMostVisited());
|
|
|
|
refreshBookmarks();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::hidePanel()
|
|
|
|
{
|
2011-04-09 00:22:50 +02:00
|
|
|
p_QupZilla->showBookmarksToolbar();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
void BookmarksToolbar::addBookmark(const BookmarksModel::Bookmark &bookmark)
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-04-15 20:45:22 +02:00
|
|
|
if (bookmark.folder != "bookmarksToolbar")
|
|
|
|
return;
|
|
|
|
QAction* action = new QAction(this);
|
|
|
|
QString title = bookmark.title;
|
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
action->setText(title);
|
|
|
|
action->setData(bookmark.url);
|
2011-04-25 20:56:45 +02:00
|
|
|
action->setIcon(_iconForUrl(bookmark.url));
|
2011-04-15 20:45:22 +02:00
|
|
|
QToolButton* button = new QToolButton(this);
|
|
|
|
button->setDefaultAction(action);
|
|
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
button->setMaximumHeight(25);
|
|
|
|
button->setToolTip(bookmark.url.toEncoded());
|
|
|
|
button->setWhatsThis(bookmark.title);
|
|
|
|
|
|
|
|
connect(action, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
|
|
|
|
insertWidget(actions().at(actions().count() - 1), button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::removeBookmark(const BookmarksModel::Bookmark &bookmark)
|
|
|
|
{
|
|
|
|
foreach (QAction* act, actions()) {
|
|
|
|
QToolButton* button = qobject_cast<QToolButton*>(widgetForAction(act));
|
|
|
|
if (!button)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QAction* action = button->actions().at(0);
|
|
|
|
if (!action)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (action->data().toUrl() == bookmark.url) {
|
|
|
|
delete button;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::bookmarkEdited(const BookmarksModel::Bookmark &before, const BookmarksModel::Bookmark &after)
|
|
|
|
{
|
|
|
|
if (before.folder == "bookmarksToolbar" && after.folder != "bookmarksToolbar") //Editing from toolbar folder to other folder -> Remove bookmark
|
|
|
|
removeBookmark(before);
|
|
|
|
else if (before.folder != "bookmarksToolbar" && after.folder == "bookmarksToolbar") //Editing from other folder to toolbar folder -> Add bookmark
|
|
|
|
addBookmark(after);
|
|
|
|
else { //Editing bookmark already in toolbar
|
|
|
|
foreach (QAction* act, actions()) {
|
|
|
|
QToolButton* button = qobject_cast<QToolButton*>(widgetForAction(act));
|
|
|
|
if (!button)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QAction* action = button->actions().at(0);
|
|
|
|
if (!action)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (action->data().toUrl() == before.url && button->whatsThis() == before.title) {
|
|
|
|
QString title = after.title;
|
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
|
|
|
|
|
|
|
action->setText(title);
|
|
|
|
action->setData(after.url);
|
2011-04-25 20:56:45 +02:00
|
|
|
action->setIcon(_iconForUrl(after.url));
|
2011-04-15 20:45:22 +02:00
|
|
|
button->setToolTip(after.url.toEncoded());
|
|
|
|
button->setWhatsThis(after.title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::refreshBookmarks()
|
|
|
|
{
|
2011-03-02 16:57:41 +01:00
|
|
|
clear();
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("SELECT title, url FROM bookmarks WHERE folder='bookmarksToolbar'");
|
|
|
|
while(query.next()) {
|
|
|
|
QUrl url = query.value(1).toUrl();
|
|
|
|
QString title = query.value(0).toString();
|
2011-04-15 20:45:22 +02:00
|
|
|
QString title_ = title;
|
2011-03-02 16:57:41 +01:00
|
|
|
QAction* action = new QAction(this);
|
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
|
|
|
|
|
|
|
action->setText(title);
|
|
|
|
action->setData(url);
|
2011-04-25 20:56:45 +02:00
|
|
|
action->setIcon(_iconForUrl(url));
|
2011-03-02 16:57:41 +01:00
|
|
|
QToolButton* button = new QToolButton(this);
|
|
|
|
button->setDefaultAction(action);
|
|
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
button->setMaximumHeight(25);
|
2011-04-15 20:45:22 +02:00
|
|
|
button->setToolTip(url.toEncoded());
|
|
|
|
button->setWhatsThis(title_);
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(action, SIGNAL(triggered()), p_QupZilla, SLOT(loadActionUrl()));
|
|
|
|
addWidget(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_bookmarksModel->isShowingMostVisited())
|
|
|
|
return;
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
m_mostVis = new QToolButton(this);
|
|
|
|
m_mostVis->setPopupMode(QToolButton::InstantPopup);
|
|
|
|
m_mostVis->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
m_mostVis->setMaximumHeight(25);
|
|
|
|
m_mostVis->setIcon(style()->standardIcon(QStyle::SP_DirIcon));
|
|
|
|
m_mostVis->setText(tr("Most visited"));
|
|
|
|
m_mostVis->setToolTip(tr("Sites You visited the most"));
|
|
|
|
|
2011-03-02 16:57:41 +01:00
|
|
|
m_menuMostVisited = new QMenu();
|
2011-04-15 20:45:22 +02:00
|
|
|
m_mostVis->setMenu(m_menuMostVisited);
|
2011-03-02 16:57:41 +01:00
|
|
|
connect(m_menuMostVisited, SIGNAL(aboutToShow()), this, SLOT(refreshMostVisited()));
|
|
|
|
|
2011-04-15 20:45:22 +02:00
|
|
|
addWidget(m_mostVis);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::refreshMostVisited()
|
|
|
|
{
|
|
|
|
m_menuMostVisited->clear();
|
|
|
|
|
|
|
|
QSqlQuery query;
|
|
|
|
query.exec("SELECT title, url FROM history ORDER BY count DESC LIMIT 10");
|
|
|
|
while(query.next()) {
|
|
|
|
QUrl url = query.value(1).toUrl();
|
|
|
|
QString title = query.value(0).toString();
|
|
|
|
if (title.length()>40) {
|
|
|
|
title.truncate(40);
|
|
|
|
title+="..";
|
|
|
|
}
|
2011-04-25 20:56:45 +02:00
|
|
|
m_menuMostVisited->addAction(_iconForUrl(url), title, p_QupZilla, SLOT(loadActionUrl()))->setData(url);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|