From 70981ab03bd0096bb44a63030c67d09041f2a256 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Sat, 5 Sep 2020 00:16:21 +0200 Subject: [PATCH] Help dialog: bookmarks are persisted now. --- src/lay/lay/layConfig.h | 1 + src/lay/lay/layHelpDialog.cc | 3 + src/lay/lay/layMainConfigPages.cc | 1 + src/lay/lay/layMainWindow.cc | 22 ++--- src/laybasic/laybasic/BrowserPanel.ui | 9 +- src/laybasic/laybasic/layBrowserPanel.cc | 114 ++++++++++++++++++++++- src/laybasic/laybasic/layBrowserPanel.h | 22 ++++- 7 files changed, 151 insertions(+), 21 deletions(-) diff --git a/src/lay/lay/layConfig.h b/src/lay/lay/layConfig.h index a915366cf..541d9f4ef 100644 --- a/src/lay/lay/layConfig.h +++ b/src/lay/lay/layConfig.h @@ -60,6 +60,7 @@ static const std::string cfg_layout_file_watcher_enabled ("layout-file-watcher-e static const std::string cfg_window_geometry ("window-geometry"); static const std::string cfg_micron_digits ("digits-micron"); static const std::string cfg_dbu_digits ("digits-dbu"); +static const std::string cfg_assistant_bookmarks ("assistant-bookmarks"); } diff --git a/src/lay/lay/layHelpDialog.cc b/src/lay/lay/layHelpDialog.cc index 8e337ee02..47f603666 100644 --- a/src/lay/lay/layHelpDialog.cc +++ b/src/lay/lay/layHelpDialog.cc @@ -24,6 +24,8 @@ #include "layHelpDialog.h" #include "layHelpSource.h" #include "layBrowserPanel.h" +#include "layDispatcher.h" +#include "layConfig.h" #include "tlStaticObjects.h" #include "ui_HelpDialog.h" @@ -49,6 +51,7 @@ HelpDialog::HelpDialog (QWidget *parent, bool modal) setModal (modal); mp_ui->button_frame->setVisible (modal); + mp_ui->browser_panel->set_dispatcher (lay::Dispatcher::instance (), cfg_assistant_bookmarks); m_def_title = windowTitle (); connect (mp_ui->browser_panel, SIGNAL (title_changed (const QString &)), this, SLOT (title_changed (const QString &))); diff --git a/src/lay/lay/layMainConfigPages.cc b/src/lay/lay/layMainConfigPages.cc index d5a219cee..7b982788b 100644 --- a/src/lay/lay/layMainConfigPages.cc +++ b/src/lay/lay/layMainConfigPages.cc @@ -83,6 +83,7 @@ public: options.push_back (std::pair (cfg_micron_digits, "5")); options.push_back (std::pair (cfg_dbu_digits, "2")); options.push_back (std::pair (cfg_reader_options_show_always, "false")); + options.push_back (std::pair (cfg_assistant_bookmarks, "")); } virtual std::vector > config_pages (QWidget *parent) const diff --git a/src/lay/lay/layMainWindow.cc b/src/lay/lay/layMainWindow.cc index 87215e52c..29383c2d4 100644 --- a/src/lay/lay/layMainWindow.cc +++ b/src/lay/lay/layMainWindow.cc @@ -192,7 +192,7 @@ MainWindow::MainWindow (QApplication *app, lay::Plugin *plugin_parent, const cha init_menu (); - mp_assistant = new lay::HelpDialog (this); + mp_assistant = 0; mp_pr = new lay::ProgressReporter (); mp_pr->set_progress_bar (&m_text_progress); @@ -3719,6 +3719,10 @@ MainWindow::cm_macro_editor () void MainWindow::cm_show_assistant () { + if (! mp_assistant) { + mp_assistant = new lay::HelpDialog (this); + } + if (mp_assistant->isMinimized ()) { mp_assistant->showNormal (); } else { @@ -3754,13 +3758,7 @@ MainWindow::show_assistant_url (const std::string &url, bool modal) } else { - if (mp_assistant->isMinimized ()) { - mp_assistant->showNormal (); - } else { - mp_assistant->show (); - } - mp_assistant->activateWindow (); - mp_assistant->raise (); + cm_show_assistant (); mp_assistant->load (url); } @@ -3777,13 +3775,7 @@ MainWindow::show_assistant_topic (const std::string &s, bool modal) } else { - if (mp_assistant->isMinimized ()) { - mp_assistant->showNormal (); - } else { - mp_assistant->show (); - } - mp_assistant->activateWindow (); - mp_assistant->raise (); + cm_show_assistant (); mp_assistant->search (s); } diff --git a/src/laybasic/laybasic/BrowserPanel.ui b/src/laybasic/laybasic/BrowserPanel.ui index 8e04d599a..d77c6c45a 100644 --- a/src/laybasic/laybasic/BrowserPanel.ui +++ b/src/laybasic/laybasic/BrowserPanel.ui @@ -142,7 +142,7 @@ - Bookmarks + Favorites @@ -462,7 +462,7 @@ - Bookmark this location + Add this location to the favorites list ... @@ -499,6 +499,11 @@ Ctrl+B + + + Delete Entry + + diff --git a/src/laybasic/laybasic/layBrowserPanel.cc b/src/laybasic/laybasic/layBrowserPanel.cc index f9a3dd172..3dd2393e2 100644 --- a/src/laybasic/laybasic/layBrowserPanel.cc +++ b/src/laybasic/laybasic/layBrowserPanel.cc @@ -22,9 +22,11 @@ #include "layBrowserPanel.h" +#include "layDispatcher.h" #include "tlExceptions.h" #include "tlInternational.h" #include "tlException.h" +#include "tlString.h" #include "ui_BrowserPanel.h" @@ -56,9 +58,44 @@ BrowserTextWidget::loadResource (int type, const QUrl &url) // ------------------------------------------------------------- +void +BookmarkItem::read (tl::Extractor &ex) +{ + while (! ex.at_end () && ! ex.test (";")) { + + std::string k, v; + ex.read_word (k); + ex.test (":"); + ex.read_word_or_quoted (v, "+-."); + ex.test (","); + + if (k == "url") { + url = v; + } else if (k == "title") { + title = v; + } else if (k == "position") { + tl::from_string (v, position); + } + + } +} + +std::string +BookmarkItem::to_string () const +{ + std::string r; + r = "url:" + tl::to_quoted_string (url) + ","; + r += "title:" + tl::to_quoted_string (title) + ","; + r += "position:" + tl::to_string (position) + ";"; + return r; +} + +// ------------------------------------------------------------- + BrowserPanel::BrowserPanel (QWidget *parent) : QWidget (parent), - m_back_dm (this, &BrowserPanel::back) + m_back_dm (this, &BrowserPanel::back), + mp_dispatcher (0) { init (); } @@ -84,6 +121,9 @@ BrowserPanel::init () mp_ui->browser->addAction (mp_ui->action_find); mp_ui->browser->addAction (mp_ui->action_bookmark); + mp_ui->browser_bookmark_view->addAction (mp_ui->action_delete_bookmark); + mp_ui->browser_bookmark_view->setContextMenuPolicy (Qt::ActionsContextMenu); + connect (mp_ui->back_pb, SIGNAL (clicked ()), this, SLOT (back ())); connect (mp_ui->forward_pb, SIGNAL (clicked ()), this, SLOT (forward ())); connect (mp_ui->next_topic_pb, SIGNAL (clicked ()), this, SLOT (next ())); @@ -103,6 +143,7 @@ BrowserPanel::init () connect (mp_ui->on_page_search_next, SIGNAL (clicked ()), this, SLOT (page_search_next ())); connect (mp_ui->action_find, SIGNAL (triggered ()), this, SLOT (find ())); connect (mp_ui->action_bookmark, SIGNAL (triggered ()), this, SLOT (bookmark ())); + connect (mp_ui->action_delete_bookmark, SIGNAL (triggered ()), this, SLOT (delete_bookmark ())); connect (mp_ui->browser_bookmark_view, SIGNAL (itemDoubleClicked (QTreeWidgetItem *, int)), this, SLOT (bookmark_item_selected (QTreeWidgetItem *))); mp_completer = new QCompleter (this); @@ -118,8 +159,6 @@ BrowserPanel::init () set_label (std::string ()); - // TODO: load bookmarks ... - refresh_bookmark_list (); } @@ -132,6 +171,37 @@ BrowserPanel::~BrowserPanel () mp_ui = 0; } +void +BrowserPanel::set_dispatcher (lay::Dispatcher *dispatcher, const std::string &cfg_bookmarks) +{ + mp_dispatcher = dispatcher; + m_cfg_bookmarks = cfg_bookmarks; + + m_bookmarks.clear (); + + // load the bookmarks + try { + + if (mp_dispatcher) { + + std::string v; + mp_dispatcher->config_get (m_cfg_bookmarks, v); + + tl::Extractor ex (v.c_str ()); + while (! ex.at_end ()) { + m_bookmarks.push_back (BookmarkItem ()); + m_bookmarks.back ().read (ex); + } + + } + + } catch (...) { + // exceptions ignored here + } + + refresh_bookmark_list (); +} + std::string BrowserPanel::title () const { @@ -155,6 +225,22 @@ BrowserPanel::bookmark () add_bookmark (bm); refresh_bookmark_list (); + store_bookmarks (); +} + +void +BrowserPanel::store_bookmarks () +{ + if (mp_dispatcher) { + + std::string s; + for (std::list::const_iterator i = m_bookmarks.begin (); i != m_bookmarks.end (); ++i) { + s += i->to_string (); + } + + mp_dispatcher->config_set (m_cfg_bookmarks, s); + + } } void @@ -178,6 +264,7 @@ BrowserPanel::bookmark_item_selected (QTreeWidgetItem *item) m_bookmarks.push_front (bm); refresh_bookmark_list (); + store_bookmarks (); load (bm.url); mp_ui->browser->verticalScrollBar ()->setValue (bm.position); @@ -204,6 +291,26 @@ BrowserPanel::add_bookmark (const BookmarkItem &item) m_bookmarks.push_front (item); } +void +BrowserPanel::delete_bookmark () +{ + QTreeWidgetItem *item = mp_ui->browser_bookmark_view->currentItem (); + if (! item) { + return; + } + + int index = mp_ui->browser_bookmark_view->indexOfTopLevelItem (item); + std::list::iterator i = m_bookmarks.begin (); + for ( ; i != m_bookmarks.end () && index > 0; --index, ++i) + ; + + if (i != m_bookmarks.end ()) { + m_bookmarks.erase (i); + refresh_bookmark_list (); + store_bookmarks (); + } +} + void BrowserPanel::refresh_bookmark_list () { @@ -213,6 +320,7 @@ BrowserPanel::refresh_bookmark_list () for (std::list::const_iterator i = m_bookmarks.begin (); i != m_bookmarks.end (); ++i) { QTreeWidgetItem *item = new QTreeWidgetItem (mp_ui->browser_bookmark_view); item->setData (0, Qt::DisplayRole, tl::to_qstring (i->title)); + item->setData (0, Qt::ToolTipRole, tl::to_qstring (i->title)); item->setData (0, Qt::DecorationRole, QIcon (":/bookmark_16.png")); } } diff --git a/src/laybasic/laybasic/layBrowserPanel.h b/src/laybasic/laybasic/layBrowserPanel.h index dd6bb194b..d32366a18 100644 --- a/src/laybasic/laybasic/layBrowserPanel.h +++ b/src/laybasic/laybasic/layBrowserPanel.h @@ -49,6 +49,7 @@ namespace lay { class BrowserPanel; +class Dispatcher; /** * @brief Specifies the outline of the document @@ -293,6 +294,9 @@ struct LAYBASIC_PUBLIC BookmarkItem return url == other.url && position == other.position; } + void read (tl::Extractor &ex); + std::string to_string () const; + std::string url; std::string title; int position; @@ -311,14 +315,26 @@ Q_OBJECT public: /** * @brief Constructor + * + * @param p The parent widget */ - BrowserPanel (QWidget *p); + BrowserPanel (QWidget *p); /** * @brief Dtor */ ~BrowserPanel (); + /** + * @brief Connects the panel to a configuration dispatcher + * + * Doing so allows storing bookmarks and retrieving them. + * + * @param dispatcher If given, this interface will be used to retrieve and store the bookmark list + * @param cfg_bookmarks If dispatcher is given, this will be the configuration key to store the bookmarks + */ + void set_dispatcher (lay::Dispatcher *dispatcher, const std::string &cfg_bookmarks); + /** * @brief Connect to a source object * If " @@ -428,6 +444,7 @@ protected slots: void text_changed (); void outline_item_clicked (QTreeWidgetItem *item); void bookmark_item_selected (QTreeWidgetItem *item); + void delete_bookmark (); protected: virtual QVariant loadResource (int type, const QUrl &url); @@ -452,11 +469,14 @@ private: QCompleter *mp_completer; QStringListModel *mp_completer_model; std::list m_bookmarks; + lay::Dispatcher *mp_dispatcher; + std::string m_cfg_bookmarks; void init (); void clear_bookmarks (); void add_bookmark (const BookmarkItem &item); void refresh_bookmark_list (); + void store_bookmarks (); }; }