2011-03-03 18:29:20 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2011-10-17 09:57:07 +02:00
|
|
|
* Copyright (C) 2010-2011 David Rosca
|
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"
|
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "bookmarksmodel.h"
|
2011-04-25 20:56:45 +02:00
|
|
|
#include "iconprovider.h"
|
2011-05-06 20:05:49 +02:00
|
|
|
#include "historymodel.h"
|
2011-09-11 19:15:06 +02:00
|
|
|
#include "toolbutton.h"
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-03-17 17:03:04 +01:00
|
|
|
BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent) :
|
2011-09-11 19:15:06 +02:00
|
|
|
QWidget(parent)
|
2011-03-02 16:57:41 +01:00
|
|
|
,p_QupZilla(mainClass)
|
2011-05-06 20:05:49 +02:00
|
|
|
,m_bookmarksModel(mApp->bookmarksModel())
|
|
|
|
,m_historyModel(mApp->history())
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
setObjectName("bookmarksbar");
|
|
|
|
m_layout = new QHBoxLayout();
|
|
|
|
m_layout->setContentsMargins(9, 3, 9, 3);
|
|
|
|
m_layout->setSpacing(0);
|
|
|
|
setLayout(m_layout);
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
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-09-11 19:15:06 +02:00
|
|
|
// QTimer::singleShot(0, this, SLOT(refreshBookmarks()));
|
|
|
|
refreshBookmarks();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::customContextMenuRequested(const QPoint &pos)
|
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
Q_UNUSED(pos)
|
2011-03-02 16:57:41 +01:00
|
|
|
|
|
|
|
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()));
|
2011-09-30 21:44:18 +02:00
|
|
|
menu.addAction(IconProvider::fromTheme("user-bookmarks"), tr("&Organize Bookmarks"), p_QupZilla, SLOT(showBookmarksManager()));
|
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);
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
void BookmarksToolbar::hidePanel()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
p_QupZilla->showBookmarksToolbar();
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
void BookmarksToolbar::loadClickedBookmark()
|
2011-03-02 16:57:41 +01:00
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
ToolButton* button = qobject_cast<ToolButton*>(sender());
|
|
|
|
if (!button)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p_QupZilla->loadAddress(button->data().toUrl());
|
|
|
|
}
|
|
|
|
|
2011-10-18 21:07:58 +02:00
|
|
|
void BookmarksToolbar::loadClickedBookmarkInNewTab()
|
|
|
|
{
|
|
|
|
ToolButton* button = qobject_cast<ToolButton*>(sender());
|
|
|
|
if (!button)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p_QupZilla->tabWidget()->addView(button->data().toUrl());
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
void BookmarksToolbar::showMostVisited()
|
|
|
|
{
|
|
|
|
m_bookmarksModel->setShowingMostVisited(!m_bookmarksModel->isShowingMostVisited());
|
2011-10-17 14:19:49 +02:00
|
|
|
m_mostVis->setVisible(!m_mostVis->isVisible());
|
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;
|
|
|
|
QString title = bookmark.title;
|
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
2011-03-02 16:57:41 +01:00
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
ToolButton* button = new ToolButton(this);
|
|
|
|
button->setText(title);
|
|
|
|
button->setData(bookmark.url);
|
|
|
|
button->setIcon(bookmark.icon);
|
2011-04-15 20:45:22 +02:00
|
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
button->setToolTip(bookmark.url.toEncoded());
|
2011-09-11 19:15:06 +02:00
|
|
|
button->setAutoRaise(true);
|
2011-04-15 20:45:22 +02:00
|
|
|
button->setWhatsThis(bookmark.title);
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
connect(button, SIGNAL(clicked()), this, SLOT(loadClickedBookmark()));
|
2011-10-18 21:07:58 +02:00
|
|
|
connect(button, SIGNAL(middleMouseClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
|
2011-09-11 19:15:06 +02:00
|
|
|
m_layout->insertWidget(m_layout->count() - 2, button);
|
2011-04-15 20:45:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::removeBookmark(const BookmarksModel::Bookmark &bookmark)
|
|
|
|
{
|
2011-09-11 19:15:06 +02:00
|
|
|
for (int i = 0; i < m_layout->count(); i++) {
|
|
|
|
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
|
2011-04-15 20:45:22 +02:00
|
|
|
if (!button)
|
|
|
|
continue;
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
if (button->data().toUrl() == bookmark.url) {
|
2011-04-15 20:45:22 +02:00
|
|
|
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
|
2011-09-11 19:15:06 +02:00
|
|
|
for (int i = 0; i < m_layout->count(); i++) {
|
|
|
|
ToolButton* button = qobject_cast<ToolButton*>(m_layout->itemAt(i)->widget());
|
2011-04-15 20:45:22 +02:00
|
|
|
if (!button)
|
|
|
|
continue;
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
if (button->data().toUrl() == before.url && button->whatsThis() == before.title) {
|
2011-04-15 20:45:22 +02:00
|
|
|
QString title = after.title;
|
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
button->setText(title);
|
|
|
|
button->setData(after.url);
|
|
|
|
button->setIcon(after.icon);
|
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
|
|
|
QSqlQuery query;
|
2011-07-30 17:57:14 +02:00
|
|
|
query.exec("SELECT title, url, icon FROM bookmarks WHERE folder='bookmarksToolbar'");
|
2011-03-02 16:57:41 +01:00
|
|
|
while(query.next()) {
|
|
|
|
QString title = query.value(0).toString();
|
2011-07-30 17:57:14 +02:00
|
|
|
QUrl url = query.value(1).toUrl();
|
|
|
|
QIcon icon = IconProvider::iconFromBase64(query.value(2).toByteArray());
|
2011-04-15 20:45:22 +02:00
|
|
|
QString title_ = title;
|
2011-03-02 16:57:41 +01:00
|
|
|
if (title.length()>15) {
|
|
|
|
title.truncate(13);
|
|
|
|
title+="..";
|
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
ToolButton* button = new ToolButton(this);
|
|
|
|
button->setText(title);
|
|
|
|
button->setData(url);
|
|
|
|
button->setIcon(icon);
|
2011-03-02 16:57:41 +01:00
|
|
|
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
2011-04-15 20:45:22 +02:00
|
|
|
button->setToolTip(url.toEncoded());
|
|
|
|
button->setWhatsThis(title_);
|
2011-09-11 19:15:06 +02:00
|
|
|
button->setAutoRaise(true);
|
|
|
|
|
|
|
|
connect(button, SIGNAL(clicked()), this, SLOT(loadClickedBookmark()));
|
2011-10-18 21:07:58 +02:00
|
|
|
connect(button, SIGNAL(middleMouseClicked()), this, SLOT(loadClickedBookmarkInNewTab()));
|
2011-09-11 19:15:06 +02:00
|
|
|
m_layout->addWidget(button);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2011-09-11 19:15:06 +02:00
|
|
|
m_mostVis = new ToolButton(this);
|
2011-04-15 20:45:22 +02:00
|
|
|
m_mostVis->setPopupMode(QToolButton::InstantPopup);
|
|
|
|
m_mostVis->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
|
|
|
|
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-09-11 19:15:06 +02:00
|
|
|
m_layout->addWidget(m_mostVis);
|
|
|
|
m_layout->addStretch();
|
2011-10-17 14:19:49 +02:00
|
|
|
|
|
|
|
m_mostVis->setVisible(m_bookmarksModel->isShowingMostVisited());
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BookmarksToolbar::refreshMostVisited()
|
|
|
|
{
|
|
|
|
m_menuMostVisited->clear();
|
|
|
|
|
2011-05-06 20:05:49 +02:00
|
|
|
QList<HistoryModel::HistoryEntry> mostList = m_historyModel->mostVisited(10);
|
|
|
|
foreach (HistoryModel::HistoryEntry entry, mostList) {
|
|
|
|
if (entry.title.length()>40) {
|
|
|
|
entry.title.truncate(40);
|
|
|
|
entry.title+="..";
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
2011-05-06 20:05:49 +02:00
|
|
|
m_menuMostVisited->addAction(_iconForUrl(entry.url), entry.title, p_QupZilla, SLOT(loadActionUrl()))->setData(entry.url);
|
2011-03-02 16:57:41 +01:00
|
|
|
}
|
|
|
|
}
|