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

[Bookmarks] BookmarksIcon & PopupWidget are now working

This commit is contained in:
nowrep 2014-02-09 12:18:52 +01:00
parent 511a6e1cd7
commit 1f4272e45e
9 changed files with 166 additions and 149 deletions

View File

@ -16,10 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "bookmarkicon.h"
#include "bookmarks.h"
#include "bookmarkswidget.h"
#include "bookmarks.h"
#include "mainapplication.h"
#include "tabbedwebview.h"
#include "webview.h"
#include "locationbar.h"
#include "pluginproxy.h"
#include "speeddial.h"
@ -29,19 +29,19 @@
BookmarkIcon::BookmarkIcon(QWidget* parent)
: ClickableLabel(parent)
, m_bookmarks(0)
, m_speedDial(mApp->plugins()->speedDial())
, m_view(0)
, m_bookmark(0)
{
setObjectName("locationbar-bookmarkicon");
setCursor(Qt::PointingHandCursor);
setToolTip(tr("Bookmark this Page"));
setFocusPolicy(Qt::ClickFocus);
m_bookmarks = mApp->bookmarks();
connect(m_bookmarks, SIGNAL(bookmarkAdded(Bookmarks::Bookmark)), this, SLOT(bookmarkAdded(Bookmarks::Bookmark)));
connect(m_bookmarks, SIGNAL(bookmarkDeleted(Bookmarks::Bookmark)), this, SLOT(bookmarkDeleted(Bookmarks::Bookmark)));
connect(m_speedDial, SIGNAL(pagesChanged()), this, SLOT(speedDialChanged()));
connect(mApp->bookmarks(), SIGNAL(bookmarkAdded(BookmarkItem*)), this, SLOT(bookmarksChanged()));
connect(mApp->bookmarks(), SIGNAL(bookmarkRemoved(BookmarkItem*)), this, SLOT(bookmarksChanged()));
connect(mApp->bookmarks(), SIGNAL(bookmarkChanged(BookmarkItem*)), this, SLOT(bookmarksChanged()));
connect(mApp->plugins()->speedDial(), SIGNAL(pagesChanged()), this, SLOT(speedDialChanged()));
connect(this, SIGNAL(clicked(QPoint)), this, SLOT(iconClicked()));
}
@ -56,7 +56,10 @@ void BookmarkIcon::checkBookmark(const QUrl &url, bool forceCheck)
return;
}
if (m_bookmarks->isBookmarked(url) || !m_speedDial->pageForUrl(url).url.isEmpty()) {
QList<BookmarkItem*> items = mApp->bookmarks()->searchBookmarks(url);
m_bookmark = items.isEmpty() ? 0 : items.first();
if (m_bookmark || !mApp->plugins()->speedDial()->pageForUrl(url).url.isEmpty()) {
setBookmarkSaved();
}
else {
@ -66,18 +69,9 @@ void BookmarkIcon::checkBookmark(const QUrl &url, bool forceCheck)
m_lastUrl = url;
}
void BookmarkIcon::bookmarkDeleted(const Bookmarks::Bookmark &bookmark)
void BookmarkIcon::bookmarksChanged()
{
if (bookmark.url == m_lastUrl) {
checkBookmark(m_lastUrl, true);
}
}
void BookmarkIcon::bookmarkAdded(const Bookmarks::Bookmark &bookmark)
{
if (bookmark.url == m_lastUrl) {
checkBookmark(m_lastUrl, true);
}
checkBookmark(m_lastUrl, true);
}
void BookmarkIcon::speedDialChanged()
@ -91,7 +85,7 @@ void BookmarkIcon::iconClicked()
return;
}
BookmarksWidget* widget = new BookmarksWidget(m_view, parentWidget());
BookmarksWidget* widget = new BookmarksWidget(m_view, m_bookmark, parentWidget());
widget->showAt(parentWidget());
}

View File

@ -21,11 +21,10 @@
#include <QUrl>
#include "clickablelabel.h"
#include "bookmarks.h"
#include "qz_namespace.h"
class SpeedDial;
class WebView;
class BookmarkItem;
class QT_QUPZILLA_EXPORT BookmarkIcon : public ClickableLabel
{
@ -37,8 +36,7 @@ public:
void checkBookmark(const QUrl &url, bool forceCheck = false);
private slots:
void bookmarkAdded(const Bookmarks::Bookmark &bookmark);
void bookmarkDeleted(const Bookmarks::Bookmark &bookmark);
void bookmarksChanged();
void speedDialChanged();
void iconClicked();
@ -50,12 +48,9 @@ private:
void setBookmarkSaved();
void setBookmarkDisabled();
Bookmarks* m_bookmarks;
SpeedDial* m_speedDial;
WebView* m_view;
BookmarkItem* m_bookmark;
QUrl m_lastUrl;
};
#endif // BOOKMARKICON_H

View File

@ -207,6 +207,30 @@ QVariantList Bookmarks::writeBookmarks(BookmarkItem* parent)
return list;
}
void Bookmarks::search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QUrl &url) const
{
Q_ASSERT(items);
Q_ASSERT(parent);
switch (parent->type()) {
case BookmarkItem::Root:
case BookmarkItem::Folder:
foreach (BookmarkItem* child, parent->children()) {
search(items, child, url);
}
break;
case BookmarkItem::Url:
if (parent->url() == url) {
items->append(parent);
}
break;
default:
break;
}
}
void Bookmarks::loadSettings()
{
Settings settings;
@ -795,6 +819,13 @@ BookmarkItem* Bookmarks::getLastFolder() const
return unsortedFolder();
}
QList<BookmarkItem*> Bookmarks::searchBookmarks(const QUrl &url) const
{
QList<BookmarkItem*> items;
search(&items, m_root, url);
return items;
}
bool Bookmarks::removeBookmark(BookmarkItem* item)
{
if (!canBeModified(item)) {

View File

@ -108,6 +108,8 @@ public:
BookmarkItem* unsortedFolder() const;
BookmarkItem* getLastFolder() const;
QList<BookmarkItem*> searchBookmarks(const QUrl &url) const;
bool canBeModified(BookmarkItem* item) const;
void addBookmark(BookmarkItem* parent, BookmarkItem* item);
@ -151,6 +153,8 @@ private:
void readBookmarks(const QVariantList &list, BookmarkItem* parent);
QVariantList writeBookmarks(BookmarkItem* parent);
void search(QList<BookmarkItem*>* items, BookmarkItem* parent, const QUrl &url) const;
bool m_showMostVisited;
bool m_showOnlyIconsInToolbar;
QString m_lastFolder;

View File

@ -107,6 +107,10 @@ void BookmarksFoldersButton::setSelectedFolder(BookmarkItem* folder)
m_selectedFolder = folder;
setText(folder->title());
if (sender()) {
emit selectedFolderChanged(folder);
}
}
void BookmarksFoldersButton::init()

View File

@ -52,10 +52,13 @@ class QT_QUPZILLA_EXPORT BookmarksFoldersButton : public QPushButton
Q_OBJECT
public:
explicit BookmarksFoldersButton(QWidget* parent, BookmarkItem* folder);
explicit BookmarksFoldersButton(QWidget* parent, BookmarkItem* folder = 0);
BookmarkItem* selectedFolder() const;
signals:
void selectedFolderChanged(BookmarkItem* folder);
public slots:
void setSelectedFolder(BookmarkItem* folder);

View File

@ -18,102 +18,73 @@
#include "bookmarkswidget.h"
#include "ui_bookmarkswidget.h"
#include "bookmarks.h"
#include "bookmarkitem.h"
#include "mainapplication.h"
#include "pluginproxy.h"
#include "speeddial.h"
#include "webview.h"
#include "qupzilla.h"
#include "bookmarkstree.h"
#include "browsinglibrary.h"
#include "bookmarksmanager.h"
#include <QToolTip>
#include <QSqlQuery>
#include <QTimer>
#define HIDE_DELAY 270
BookmarksWidget::BookmarksWidget(WebView* view, QWidget* parent)
BookmarksWidget::BookmarksWidget(WebView* view, BookmarkItem* bookmark, QWidget* parent)
: LocationBarPopup(parent)
, ui(new Ui::BookmarksWidget)
, m_url(view->url())
, m_view(view)
, m_bookmark(bookmark)
, m_bookmarks(mApp->bookmarks())
, m_speedDial(mApp->plugins()->speedDial())
, m_edited(false)
{
ui->setupUi(this);
m_bookmarksTree = new BookmarksTree(this);
m_bookmarksTree->setViewType(BookmarksTree::ComboFolderView);
m_bookmarksTree->header()->hide();
m_bookmarksTree->setColumnCount(1);
ui->folder->setModel(m_bookmarksTree->model());
ui->folder->setView(m_bookmarksTree);
// The locationbar's direction is direction of its text,
// it dynamically changes and so, it's not good choice for this widget.
setLayoutDirection(QApplication::layoutDirection());
connect(ui->speeddialButton, SIGNAL(clicked()), this, SLOT(toggleSpeedDial()));
const SpeedDial::Page page = m_speedDial->pageForUrl(m_url);
ui->speeddialButton->setFlat(page.url.isEmpty() ? true : false);
ui->speeddialButton->setText(page.url.isEmpty() ?
tr("Add to Speed Dial") :
tr("Remove from Speed Dial"));
loadBookmark();
connect(ui->folder, SIGNAL(activated(int)), this, SLOT(comboItemActive(int)));
connect(m_bookmarksTree, SIGNAL(requestNewFolder(QWidget*,QString*,bool,QString,WebView*)),
mApp->browsingLibrary()->bookmarksManager(), SLOT(addFolder(QWidget*,QString*,bool,QString,WebView*)));
init();
}
void BookmarksWidget::loadBookmark()
BookmarksWidget::~BookmarksWidget()
{
// Bookmark folders
m_bookmarksTree->refreshTree();
Bookmarks::Bookmark bookmark = m_bookmarks->getBookmark(m_bookmarkId);
m_bookmarkId = bookmark.id;
if (m_bookmarkId > 0) {
int index = ui->folder->findData(bookmark.folder);
// QComboBox::findData() returns index related to the item's parent
if (index == -1) { // subfolder
QModelIndex rootIndex = ui->folder->rootModelIndex();
ui->folder->setRootModelIndex(ui->folder->model()->index(ui->folder->findText(_bookmarksToolbar), 0));
// subfolder's name and its stored data are the same
index = ui->folder->findText(bookmark.folder);
ui->folder->setCurrentIndex(index);
ui->folder->setRootModelIndex(rootIndex);
}
else {
ui->folder->setCurrentIndex(index);
}
ui->saveRemove->setText(tr("Remove from Bookmarks"));
ui->saveRemove->setFlat(false);
connect(ui->folder, SIGNAL(currentIndexChanged(int)), SLOT(bookmarkEdited()));
}
else {
ui->folder->setCurrentIndex(0);
}
delete ui;
}
void BookmarksWidget::toggleSpeedDial()
{
const SpeedDial::Page page = m_speedDial->pageForUrl(m_url);
const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
if (page.url.isEmpty()) {
QString title = m_view->title();
m_speedDial->addPage(m_url, title);
m_speedDial->addPage(m_view->url(), title);
}
else {
m_speedDial->removePage(page);
}
QTimer::singleShot(HIDE_DELAY, this, SLOT(close()));
closePopup();
}
void BookmarksWidget::toggleBookmark()
{
if (m_bookmark) {
if (m_edited) {
// Change folder
m_bookmarks->removeBookmark(m_bookmark);
m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), m_bookmark);
}
else {
// Remove
m_bookmarks->removeBookmark(m_bookmark);
}
}
else {
// Save bookmark
BookmarkItem* bookmark = new BookmarkItem(BookmarkItem::Url);
bookmark->setTitle(m_view->title());
bookmark->setUrl(m_view->url());
m_bookmarks->addBookmark(ui->folderButton->selectedFolder(), bookmark);
}
closePopup();
}
void BookmarksWidget::bookmarkEdited()
@ -123,34 +94,48 @@ void BookmarksWidget::bookmarkEdited()
}
m_edited = true;
ui->saveRemove->setFlat(true);
ui->bookmarksButton->setText(tr("Update Bookmark"));
ui->bookmarksButton->setFlat(true);
}
void BookmarksWidget::comboItemActive(int index)
void BookmarksWidget::init()
{
m_bookmarksTree->activeItemChange(index, ui->folder, m_view->title(), m_view);
}
// The locationbar's direction is direction of its text,
// it dynamically changes and so, it's not good choice for this widget.
setLayoutDirection(QApplication::layoutDirection());
void BookmarksWidget::on_saveRemove_clicked(bool)
{
if (m_bookmarkId > 0) {
if (m_edited) {
m_bookmarks->editBookmark(m_bookmarkId, m_view->title(), QUrl(), Bookmarks::fromTranslatedFolder(ui->folder->currentText()));
}
else {
m_bookmarks->removeBookmark(m_bookmarkId);
emit bookmarkDeleted();
}
// Init SpeedDial button
const SpeedDial::Page page = m_speedDial->pageForUrl(m_view->url());
if (page.url.isEmpty()) {
ui->speeddialButton->setFlat(true);
ui->speeddialButton->setText(tr("Add to Speed Dial"));
}
else {
m_bookmarks->saveBookmark(m_url, m_view->title(), m_view->icon(), Bookmarks::fromTranslatedFolder(ui->folder->currentText()));
ui->speeddialButton->setFlat(false);
ui->speeddialButton->setText(tr("Remove from Speed Dial"));
}
// Init Bookmarks button
if (m_bookmark) {
ui->bookmarksButton->setText(tr("Remove from Bookmarks"));
ui->bookmarksButton->setFlat(false);
Q_ASSERT(m_bookmark->parent());
ui->folderButton->setSelectedFolder(m_bookmark->parent());
connect(ui->folderButton, SIGNAL(selectedFolderChanged(BookmarkItem*)), SLOT(bookmarkEdited()));
}
connect(ui->speeddialButton, SIGNAL(clicked()), this, SLOT(toggleSpeedDial()));
connect(ui->bookmarksButton, SIGNAL(clicked()), this, SLOT(toggleBookmark()));
}
void BookmarksWidget::closePopup()
{
// Prevent clicking again on buttons while popup is being closed
disconnect(ui->speeddialButton, SIGNAL(clicked()), this, SLOT(toggleSpeedDial()));
disconnect(ui->bookmarksButton, SIGNAL(clicked()), this, SLOT(toggleBookmark()));
QTimer::singleShot(HIDE_DELAY, this, SLOT(close()));
}
BookmarksWidget::~BookmarksWidget()
{
delete ui;
}

View File

@ -18,7 +18,6 @@
#ifndef BOOKMARKSWIDGET_H
#define BOOKMARKSWIDGET_H
#include <QUrl>
#include <QMenu>
#include "qz_namespace.h"
@ -32,37 +31,31 @@ class BookmarksWidget;
class WebView;
class SpeedDial;
class Bookmarks;
class BookmarksTree;
class BookmarkItem;
class QT_QUPZILLA_EXPORT BookmarksWidget : public LocationBarPopup
{
Q_OBJECT
public:
explicit BookmarksWidget(WebView* view, QWidget* parent = 0);
explicit BookmarksWidget(WebView* view, BookmarkItem* bookmark, QWidget* parent = 0);
~BookmarksWidget();
signals:
void bookmarkDeleted();
private slots:
void on_saveRemove_clicked(bool);
void bookmarkEdited();
void comboItemActive(int index);
void toggleSpeedDial();
void toggleBookmark();
void bookmarkEdited();
private:
void loadBookmark();
void init();
void closePopup();
Ui::BookmarksWidget* ui;
QUrl m_url;
int m_bookmarkId;
WebView* m_view;
BookmarkItem* m_bookmark;
Bookmarks* m_bookmarks;
SpeedDial* m_speedDial;
bool m_edited;
BookmarksTree* m_bookmarksTree;
};
#endif // BOOKMARKSWIDGET_H

View File

@ -2,10 +2,32 @@
<ui version="4.0">
<class>BookmarksWidget</class>
<widget class="QWidget" name="BookmarksWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>167</width>
<height>89</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QPushButton" name="bookmarksButton">
<property name="text">
<string>Add to bookmarks</string>
</property>
<property name="icon">
<iconset resource="../data/icons.qrc">
<normaloff>:/icons/other/bigstar.png</normaloff>:/icons/other/bigstar.png</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QPushButton" name="speeddialButton">
<property name="focusPolicy">
@ -23,32 +45,18 @@
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="saveRemove">
<property name="text">
<string>Add to bookmarks</string>
</property>
<property name="icon">
<iconset resource="../data/icons.qrc">
<normaloff>:/icons/other/bigstar.png</normaloff>:/icons/other/bigstar.png</iconset>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QComboBox" name="folder">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
<widget class="BookmarksFoldersButton" name="folderButton"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>BookmarksFoldersButton</class>
<extends>QPushButton</extends>
<header>bookmarkstools.h</header>
</customwidget>
</customwidgets>
<resources>
<include location="../data/icons.qrc"/>
</resources>