Commits

Ivan Andrianov committed 06626e9

"Create bookmark to current tab" and "Create bookmarks folder" actions added to bookmarks menu

committer: PayableOnDeath <>

Comments (0)

Files changed (4)

icons/createFolder.png

Added
New image
         <file>copy.png</file>
         <file>createBookmark.png</file>
         <file>folder.png</file>
+        <file>createFolder.png</file>
     </qresource>
 </RCC>
 #include "historydialog.h"
 #include "bookmarksmanager.h"
 #include "bookmarksitem.h"
+#include "tab.h"
+#include "webview.h"
 
 HistoryDialog * ToolBar::m_historyDialog = 0;
 int ToolBar::m_count = 0;
     return m_adressBar->url();
 }
 
-void ToolBar::fillBookmarksMenuPart(QMenu * menu, BookmarksItem * item)
+void ToolBar::fillBookmarksMenuPart(QMenu * menu,
+                                    BookmarksItem * item,
+                                    QList<QVariant> indexes)
 {
     if(item->url() != QUrl())
     {
     {
         QMenu * newMenu = menu->addMenu(QIcon(":/folder.png"), item->title());
 
+        QAction * createBookmarkAction =
+                newMenu->
+                addAction(QIcon(":/createBookmark.png"),
+                          trUtf8("Create bookmark to current tab"));
+        createBookmarkAction->setData(indexes);
+        connect(createBookmarkAction, SIGNAL(triggered()),
+                this, SLOT(createBookmark()));
+
+        QAction * createFolderAction =
+                newMenu->
+                addAction(QIcon(":/createFolder.png"),
+                          trUtf8("Create new bookmarks folder"));
+        createFolderAction->setData(indexes);
+        connect(createFolderAction, SIGNAL(triggered()),
+                this, SLOT(createFolder()));
+
+        newMenu->addSeparator();
+
         for(int i = 0; i < item->childrenCount(); i++)
         {
             BookmarksItem * newItem = item->child(i);
-            fillBookmarksMenuPart(newMenu, newItem);
+            QList<QVariant> newIndexes = indexes;
+            newIndexes += i;
+            fillBookmarksMenuPart(newMenu, newItem, newIndexes);
         }
     }
 }
 {
     m_bookmarksMenu->clear();
 
+    QAction * createBookmarkAction =
+            m_bookmarksMenu->
+            addAction(QIcon(":/createBookmark.png"),
+                      trUtf8("Create bookmark to current tab"));
+    createBookmarkAction->setData(QList<QVariant>());
+    connect(createBookmarkAction, SIGNAL(triggered()),
+            this, SLOT(createBookmark()));
+
+    QAction * createFolderAction =
+            m_bookmarksMenu->
+            addAction(QIcon(":/createFolder.png"),
+                      trUtf8("Create new bookmarks folder"));
+    createFolderAction->setData(QList<QVariant>());
+    connect(createFolderAction, SIGNAL(triggered()),
+            this, SLOT(createFolder()));
+
+    m_bookmarksMenu->addSeparator();
+
     BookmarksManager * manager = MainWindow::bookmarksManager();
 
     for(int i = 0; i < manager->count(); i++)
     {
+        QList<QVariant> indexes;
+        indexes += i;
         BookmarksItem * item = manager->item(i);
-        fillBookmarksMenuPart(m_bookmarksMenu, item);
+        fillBookmarksMenuPart(m_bookmarksMenu, item, indexes);
     }
 }
 
         MainWindow::tabManager()->addTab(action->data().toUrl());
     }
 }
+
+void ToolBar::createBookmark()
+{
+    QAction * action = qobject_cast<QAction *>(sender());
+    if(action)
+    {
+        BookmarksManager * manager = MainWindow::bookmarksManager();
+        QList<QVariant> indexes = action->data().toList();
+        BookmarksItem * parent;
+        if(indexes.isEmpty())
+        {
+            parent = 0;
+        }
+        else
+        {
+            parent = manager->item(indexes.at(0).toInt());
+            for(int i = 1; i < indexes.count(); i++)
+            {
+                parent = parent->child(indexes.at(i).toInt());
+            }
+        }
+        Tab * tab = qobject_cast<Tab *>(parentWidget());
+        if(tab)
+        {
+            BookmarksItem * item = new BookmarksItem(url(), manager);
+            item->setTitle(tab->webView()->title());
+            manager->addItem(item, parent);
+        }
+    }
+}
+
+void ToolBar::createFolder()
+{
+    QAction * action = qobject_cast<QAction *>(sender());
+    if(action)
+    {
+        BookmarksManager * manager = MainWindow::bookmarksManager();
+        QList<QVariant> indexes = action->data().toList();
+        BookmarksItem * parent;
+        if(indexes.isEmpty())
+        {
+            parent = 0;
+        }
+        else
+        {
+            parent = manager->item(indexes.at(0).toInt());
+            for(int i = 1; i < indexes.count(); i++)
+            {
+                parent = parent->child(indexes.at(i).toInt());
+            }
+        }
+        bool ok;
+        QString name =
+                QInputDialog::getText(this, trUtf8("Bookmarks"),
+                                      trUtf8("Enter new folder name:"),
+                                      QLineEdit::Normal, QString(), &ok);
+        if(ok && !name.isEmpty())
+        {
+            BookmarksItem * item = new BookmarksItem(QUrl(), manager);
+            item->setTitle(name);
+            manager->addItem(item, parent);
+        }
+    }
+}
 class HistoryDialog;
 class BookmarksManager;
 class BookmarksItem;
+class Tab;
+class WebView;
 
 class ToolBar
         : public QToolBar
     QAction * m_showHistoryAction;
     static HistoryDialog * m_historyDialog;
     static int m_count;
-    void fillBookmarksMenuPart(QMenu * menu, BookmarksItem * item);
+    void fillBookmarksMenuPart(QMenu * menu, BookmarksItem * item, QList<QVariant> indexes);
 
 public slots:
     void setUrl(const QUrl & url);
     void navigate();
     void restore();
     void openBookmark();
+    void createBookmark();
+    void createFolder();
 
 signals:
     void goBackRequsted();