Help dialog: bookmarks are persisted now.

This commit is contained in:
Matthias Koefferlein 2020-09-05 00:16:21 +02:00
parent 6bfed45b5d
commit 70981ab03b
7 changed files with 151 additions and 21 deletions

View File

@ -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");
}

View File

@ -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 &)));

View File

@ -83,6 +83,7 @@ public:
options.push_back (std::pair<std::string, std::string> (cfg_micron_digits, "5"));
options.push_back (std::pair<std::string, std::string> (cfg_dbu_digits, "2"));
options.push_back (std::pair<std::string, std::string> (cfg_reader_options_show_always, "false"));
options.push_back (std::pair<std::string, std::string> (cfg_assistant_bookmarks, ""));
}
virtual std::vector<std::pair <std::string, ConfigPage *> > config_pages (QWidget *parent) const

View File

@ -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);
}

View File

@ -142,7 +142,7 @@
</property>
<column>
<property name="text">
<string>Bookmarks</string>
<string>Favorites</string>
</property>
</column>
</widget>
@ -462,7 +462,7 @@
<item row="0" column="9">
<widget class="QToolButton" name="bookmark_pb">
<property name="toolTip">
<string>Bookmark this location</string>
<string>Add this location to the favorites list</string>
</property>
<property name="text">
<string>...</string>
@ -499,6 +499,11 @@
<string>Ctrl+B</string>
</property>
</action>
<action name="action_delete_bookmark">
<property name="text">
<string>Delete Entry</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>

View File

@ -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<BookmarkItem>::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<BookmarkItem>::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<BookmarkItem>::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"));
}
}

View File

@ -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<BookmarkItem> 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 ();
};
}