-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlg_bookmarks.cc
759 lines (670 loc) · 24.7 KB
/
dlg_bookmarks.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* ----------------------------------------------------------------------------
* Copyright (C) 2007-2010,2020 Th. Zoerner
* ----------------------------------------------------------------------------
* 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/>.
* ----------------------------------------------------------------------------
*
* Module description:
*
* This module implements the "bookmark" dialog. The dialog shows a list of
* text lines, selected by the user from the main document. When clicking on of
* the lines, the respective line is shown & selected in the main window and
* the search list dialog. New bookmarks can be added via the main window or
* search window (keyboard shortcut "m", or via selection & a menu command.)
* The bookmark list can thus be seen as another level of filtering above
* search lists (i.e. the user selects a usually large number of lines for the
* search list, and from among these selected for the bookmark list.)
*
* The dialog allows editing the list by removing entries, or changing the
* bookmark label (which by default is simply a copy of the text of the line.)
* Also the list can be save to or read from a file.
*/
#include <QWidget>
#include <QApplication>
#include <QDesktopWidget>
#include <QTableView>
#include <QAbstractItemModel>
#include <QItemSelection>
#include <QHeaderView>
#include <QVBoxLayout>
#include <QPushButton>
#include <QLabel>
#include <QMenu>
#include <QMessageBox>
#include <QDialogButtonBox>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <cstdio>
#include <string>
#include <vector>
#include "main_win.h"
#include "main_text.h"
#include "main_search.h"
#include "config_file.h"
#include "status_line.h"
#include "search_list.h"
#include "bookmarks.h"
#include "highlighter.h"
#include "highl_view_dlg.h"
#include "dlg_bookmarks.h"
// ----------------------------------------------------------------------------
class DlgBookmarkModel : public QAbstractItemModel, public HighlightViewModelIf
{
public:
enum TblColIdx { COL_IDX_LINE, COL_IDX_TEXT, COL_COUNT };
DlgBookmarkModel(MainText * mainText, Highlighter * higl, Bookmarks * bookmarks)
: m_mainText(mainText)
, m_higl(higl)
, m_bookmarks(bookmarks)
{
m_bookmarks->getLineList(m_lines);
}
virtual ~DlgBookmarkModel() = default;
virtual QModelIndex index(int row, int column, const QModelIndex& parent __attribute__((unused)) = QModelIndex()) const override
{
return createIndex(row, column);
}
virtual QModelIndex parent(const QModelIndex&) const override
{
return QModelIndex();
}
virtual int rowCount(const QModelIndex& parent __attribute__((unused)) = QModelIndex()) const override
{
return m_lines.size();
}
virtual int columnCount(const QModelIndex& parent __attribute__((unused)) = QModelIndex()) const override
{
return COL_COUNT;
}
virtual Qt::ItemFlags flags(const QModelIndex& index __attribute__((unused))) const override
{
if (index.column() == COL_IDX_TEXT)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
virtual QVariant headerData(int section __attribute__((unused)), Qt::Orientation orientation __attribute__((unused)), int role __attribute__((unused))) const override;
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
virtual bool setData(const QModelIndex& index, const QVariant &value, int role = Qt::EditRole) override;
//virtual bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
//virtual bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
int lineCount() const
{
return m_lines.size();
}
int getLineIdx(int line) const
{
auto it = std::find(m_lines.begin(), m_lines.end(), line);
return ((it != m_lines.end()) ? (it - m_lines.begin()) : -1);
}
bool getLineIdx(int line, int &idx) const
{
auto it = std::find(m_lines.begin(), m_lines.end(), line);
idx = ((it != m_lines.end()) ? (it - m_lines.begin()) : -1);
return (it != m_lines.end());
}
// reverse lookup of getLineIdx
int getLineOfIdx(int row) const
{
return ((size_t(row) < m_lines.size()) ? m_lines[row] : -1);
}
void refreshContents();
void forceRedraw(TblColIdx col, int line = -1);
// implementation of HighlightViewModelIf interfaces
virtual const HiglFmtSpec * getFmtSpec(const QModelIndex& index) const override
{
int line = getLineOfIdx(index.row());
if (line >= 0)
return m_higl->getFmtSpecForLine(line, true, true);
else
return nullptr;
}
virtual QVariant higlModelData(const QModelIndex& index, int role = Qt::DisplayRole) const override
{
return data(index, role);
}
private:
MainText * const m_mainText;
Highlighter * const m_higl;
Bookmarks * const m_bookmarks;
std::vector<int> m_lines;
};
QVariant DlgBookmarkModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal)
{
switch (section)
{
case COL_IDX_LINE: return QVariant("Line number");
case COL_IDX_TEXT: return QVariant("Content / Description");
default: break;
}
qWarning("Invalid index:%d for headerData()", section);
}
else
{
if (size_t(section) < m_lines.size())
{
return QVariant(QString::number(section + 1));
}
}
}
return QVariant();
}
QVariant DlgBookmarkModel::data(const QModelIndex &index, int role) const
{
if ( ((role == Qt::DisplayRole) || (role == Qt::EditRole))
&& (size_t(index.row()) < m_lines.size()))
{
int line = m_lines[index.row()];
switch (index.column())
{
case COL_IDX_LINE: return QVariant(line + 1);
case COL_IDX_TEXT: return QVariant(m_bookmarks->getText(line));
default: break;
}
qWarning("Invalid index:%d for data()", index.column());
}
return QVariant();
}
bool DlgBookmarkModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
{
int row = index.row();
int col = index.column();
bool result = false;
if (size_t(row) < m_lines.size())
{
if (col == COL_IDX_TEXT)
{
QString str = value.toString();
int line = m_lines[row];
if (!str.isEmpty() && (str != m_bookmarks->getText(line)))
{
m_bookmarks->setText(line, str);
emit dataChanged(index, index);
result = true;
}
}
else
qWarning("setData() for non-editable column %d", col);
}
return result;
}
// called when external state has changed that affects rendering
void DlgBookmarkModel::forceRedraw(TblColIdx col, int line) // ATTN row/col order reverse of usual
{
if (m_lines.size() != 0)
{
int idx;
if (line < 0)
{
emit dataChanged(createIndex(0, col),
createIndex(m_lines.size() - 1, col));
}
else if (getLineIdx(line, idx))
{
auto midx = createIndex(idx, col);
emit dataChanged(midx, midx);
}
}
}
// called after removal/insertion of bookmarks
// UNCLEAN but used due to loose coupling with bookmarks database
// (in particular items can be inserted/removed by other dialogs)
void DlgBookmarkModel::refreshContents()
{
m_lines.clear();
m_bookmarks->getLineList(m_lines);
emit layoutChanged();
}
// ----------------------------------------------------------------------------
class DlgBookmarkView : public QTableView
{
public:
DlgBookmarkView(QWidget *parent)
: QTableView(parent)
{
}
virtual void keyPressEvent(QKeyEvent *e) override;
};
void DlgBookmarkView::keyPressEvent(QKeyEvent *e)
{
switch (e->key())
{
// Firstly make Home key behave same with and without CTRL
// Secondly correct behavior so that target line is selected
case Qt::Key_Home:
if (model()->rowCount() != 0)
{
QModelIndex midx = model()->index(0, 0);
this->setCurrentIndex(midx);
}
break;
case Qt::Key_End:
if (int count = model()->rowCount())
{
QModelIndex midx = model()->index(count - 1, 0);
this->setCurrentIndex(midx);
}
break;
default:
QTableView::keyPressEvent(e);
break;
}
}
// ----------------------------------------------------------------------------
/**
* This function creates the dialog window showing all currently defined
* bookmarks.
*/
DlgBookmarks::DlgBookmarks()
{
auto central_wid = new QWidget();
setCentralWidget(central_wid);
auto layout_top = new QVBoxLayout(central_wid);
m_model = new DlgBookmarkModel(s_mainText, s_higl, s_bookmarks);
m_draw = new HighlightViewDelegate(m_model, false,
s_mainText->getFontContent(),
s_mainText->getFgColDefault(),
s_mainText->getBgColDefault());
// main dialog component: table view, showing a list of user-defined patterns
QFontMetrics metrics1(s_mainText->getFontContent());
m_table = new DlgBookmarkView(central_wid);
m_table->setModel(m_model);
m_table->setShowGrid(false);
m_table->horizontalHeader()->setSectionResizeMode(DlgBookmarkModel::COL_IDX_TEXT, QHeaderView::Stretch);
m_table->verticalHeader()->setVisible(false);
m_table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
QFontMetrics metrics2(m_table->font());
m_table->verticalHeader()->setDefaultSectionSize(std::max(metrics1.height(), metrics2.height()));
m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
m_table->setContextMenuPolicy(Qt::CustomContextMenu);
m_table->setItemDelegateForColumn(DlgBookmarkModel::COL_IDX_TEXT, m_draw);
connect(m_table, &QAbstractItemView::customContextMenuRequested, this, &DlgBookmarks::showContextMenu);
connect(m_table->selectionModel(), &QItemSelectionModel::selectionChanged, this, &DlgBookmarks::selectionChanged);
layout_top->addWidget(m_table);
// Command buttons at the bottom of the dialog window: Close, "File..."
m_cmdButs = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, central_wid);
connect(m_cmdButs, &QDialogButtonBox::clicked, this, &DlgBookmarks::cmdButton);
auto fileMenu = new QMenu("File actions", this);
connect(fileMenu, &QMenu::aboutToShow, [=](){ m_saveMenuAct->setEnabled(s_bookmarks->validFileName()); });
m_saveMenuAct = fileMenu->addAction("Save");
connect(m_saveMenuAct, &QAction::triggered, [=](){ s_bookmarks->saveFileAs(this, true); });
auto act = fileMenu->addAction("Save as...");
connect(act, &QAction::triggered, [=](){ s_bookmarks->saveFileAs(this, false); });
act = fileMenu->addAction("Load...");
connect(act, &QAction::triggered, [=](){ s_bookmarks->readFileFrom(this); });
auto but = m_cmdButs->addButton("File...", QDialogButtonBox::ActionRole);
but->setMenu(fileMenu);
layout_top->addWidget(m_cmdButs);
// Status line overlay: used to display notification text
m_stline = new StatusLine(m_table);
connect(s_mainText, &MainText::textFontChanged, this, &DlgBookmarks::mainFontChanged);
connect(s_mainWin, &MainWin::documentNameChanged, this, &DlgBookmarks::mainDocNameChanged);
mainDocNameChanged(); // set window title initially
act = new QAction(central_wid);
act->setShortcut(QKeySequence(Qt::Key_Delete));
connect(act, &QAction::triggered, this, &DlgBookmarks::cmdRemove);
central_wid->addAction(act);
act = new QAction(central_wid);
act->setShortcut(QKeySequence(Qt::Key_Escape));
connect(act, &QAction::triggered, this, &DlgBookmarks::cmdClose);
central_wid->addAction(act);
if (!s_winGeometry.isEmpty() && !s_winState.isEmpty())
{
this->restoreGeometry(s_winGeometry);
this->restoreState(s_winState);
}
else
{
// set reasonable default size when starting without RC file
QDesktopWidget dw;
this->resize(dw.width()*0.75, dw.height()*0.33);
}
m_table->setFocus(Qt::ShortcutFocusReason);
this->show();
}
/**
* Destructor: Freeing resources not automatically deleted via widget tree
*/
DlgBookmarks::~DlgBookmarks()
{
delete m_model;
delete m_draw;
}
/**
* This overriding function is called when the dialog window receives the close
* event. The function stops background processes and destroys the class
* instance to release all resources. The event is always accepted.
*/
void DlgBookmarks::closeEvent(QCloseEvent * event)
{
s_winGeometry = this->saveGeometry();
s_winState = this->saveState();
ConfigFile::updateRcAfterIdle();
event->accept();
s_instance->deleteLater();
s_instance = nullptr;
}
/**
* This slot is connected to the ESCAPE key shortcut. It closes the dialog in
* the same way as via the X button in the window title bar.
*/
void DlgBookmarks::cmdClose(bool)
{
QCoreApplication::postEvent(this, new QCloseEvent());
}
/**
* This slot is connected to notification of font changes in the main text
* window, as the same font is used for displaying text content in the dialog.
* The function resizes row height and then forces a redraw. Note the new font
* need not be passed to the view delegate as it gets passed a reference to the
* font so that it picks up the new font automatically.
*/
void DlgBookmarks::mainFontChanged()
{
QFontMetrics metrics1(s_mainText->getFontContent());
QFontMetrics metrics2(m_table->font());
m_table->verticalHeader()->setDefaultSectionSize(std::max(metrics1.height(), metrics2.height()));
m_model->forceRedraw(DlgBookmarkModel::COL_IDX_TEXT);
}
/**
* This slot is connected to notification of main document file changes. This
* is used to update the dialog window title. (Note before this event, the
* dialog is informed separately about discarding the content of the previous
* document.)
*/
void DlgBookmarks::mainDocNameChanged()
{
const QString& fileName = s_mainWin->getFilename();
if (!fileName.isEmpty())
this->setWindowTitle("Bookmark list - " + fileName);
else
this->setWindowTitle("Bookmark list - trowser");
}
/**
* This slot is bound to the main command buttons: In this dialog this is only
* the "Close" button.
*/
void DlgBookmarks::cmdButton(QAbstractButton * button)
{
if (button == m_cmdButs->button(QDialogButtonBox::Close))
{
QCoreApplication::postEvent(this, new QCloseEvent());
}
}
/**
* This function pops up a context menu for the bookmarks list.
*/
void DlgBookmarks::showContextMenu(const QPoint& pos)
{
auto menu = new QMenu("Bookmark actions", this);
auto sel = m_table->selectionModel()->selectedRows();
auto act = menu->addAction("Remove selected entries");
act->setEnabled(sel.size() != 0);
connect(act, &QAction::triggered, this, &DlgBookmarks::cmdRemove);
menu->addSeparator();
act = menu->addAction("Rename bookmark");
act->setEnabled(sel.size() != 0);
if (sel.size() != 0)
connect(act, &QAction::triggered, [=](){ m_table->edit(m_model->index(sel.front().row(), DlgBookmarkModel::COL_IDX_TEXT)); });
menu->exec(mapToGlobal(pos));
}
/**
* This external static interface function is called out of the main window's
* highlight loop for every line to which a search-match highlight is applied.
* If the search list dialog is not open the function does nothing. Else it
* forces a redraw of the text column in the given line so that the changed
* highlighting is applied. (The model internally queries the main window
* highlighting database so no local state change is required.)
*/
void DlgBookmarks::signalHighlightLine(int line) /*static*/
{
if (s_instance != nullptr)
{
s_instance->m_model->forceRedraw(DlgBookmarkModel::COL_IDX_TEXT, line);
}
}
/**
* This external static interface is called by the highlighting class after a
* global change to highlighting (i.e. either global search highlight on/off,
* or change of mark-up). The notification is ignored if the bookmark dialog is
* not open; else it forces a refresh of the text column. (The model
* internally queries the main window highlighting database so no local state
* change is required.)
*/
void DlgBookmarks::signalHighlightReconfigured() /*static*/
{
if (s_instance != nullptr)
{
s_instance->m_model->forceRedraw(DlgBookmarkModel::COL_IDX_TEXT);
}
}
/**
* This callback is invoked when the search history list has changed externally
* (i.e. items removed/added or modified). The function first forces a
* complete redraw of the contents. Then it will re-select all previously
* selected items, specifically those with the same pattern string.
*
* Note this special handling is necessary because by default the selection
* model would keep selection at the same indices, which now may refer to
* completely different items.
*/
void DlgBookmarks::refreshContents()
{
m_model->refreshContents();
QItemSelection sel;
QModelIndex first;
for (int row = 0; row < m_model->rowCount(); ++row)
{
if (m_selPats.find(m_model->getLineOfIdx(row)) != m_selPats.end())
{
QModelIndex midx = m_model->index(row, 0);
sel.select(midx, midx);
//newSelPats.insert(m_model->getLineOfIdx(row));
if (first.isValid() == false)
first = midx;
}
}
if (sel != m_table->selectionModel()->selection())
{
// Cannot rely on selection update below to invoke the callback, so update here directly;
// Must be done first as list gets updated during following selection change.
//m_selPats = newSelPats;
if (first.isValid())
m_table->setCurrentIndex(first);
m_table->selectionModel()->select(sel, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
}
}
/**
* This external static interface is called by the Bookmarks management class
* for every addition or removal of a bookmark. Currently this simply forces a
* complete refresh (see explanation above).
*/
void DlgBookmarks::signalBookmarkListChanged() /*static*/
{
if (s_instance != nullptr)
{
s_instance->refreshContents();
}
}
/**
* This function adjusts the view in the bookmark list so that the given text
* line becomes visible. Nothing happens if the line is not in the list.
*/
void DlgBookmarks::matchViewInt(int line)
{
int idx;
if (m_model->getLineIdx(line, idx))
{
// ignore selection chage callback triggered by following sel. changes
m_ignoreSelCb = idx;
// move selection onto the line; clear selection if line is not in the list
QModelIndex midx = m_model->index(idx, 0);
m_table->setCurrentIndex(midx);
}
}
/**
* This external static interface is called by search list and certain cursor
* placement functions in the main text window to synchronize the view in the
* bookmarks list with theirs. Specifically, it places the cursor (selection)
* onto the same line, if it is in the bookmark list.
*/
void DlgBookmarks::matchView(int line) /*static*/
{
if (s_instance != 0)
{
s_instance->matchViewInt(line);
}
}
/**
* This function is bound to changes of the selection in the color tags list.
*/
void DlgBookmarks::selectionChanged(const QItemSelection& /*selected*/, const QItemSelection& /*deselected*/)
{
auto sel = m_table->selectionModel()->selectedRows();
if (sel.size() == 1)
{
if (m_ignoreSelCb == sel.front().row())
{
// FIXME WA for ignoring callback triggered by matchViewInt()
}
else
{
int line = m_model->getLineOfIdx(sel.front().row());
if (line >= 0)
{
s_search->highlightFixedLine(line);
SearchList::matchView(line);
}
}
}
m_ignoreSelCb = -1;
// keep copy of selected patterns to maintain selection across list updates
m_selPats.clear();
for (auto midx : m_table->selectionModel()->selectedRows())
{
m_selPats.insert(m_model->getLineOfIdx(midx.row()));
}
}
/**
* This function is bound to the "Remove selected lines" command in the
* search history list dialog's context menu. All currently selected text
* lines are removed from the search list.
*/
void DlgBookmarks::cmdRemove(bool)
{
auto sel = m_table->selectionModel()->selectedRows();
if (sel.size() != 0)
{
m_stline->clearMessage("bookmarks");
std::vector<int> lineList;
lineList.reserve(sel.size());
for (auto midx : sel)
{
lineList.push_back(m_model->getLineOfIdx(midx.row()));
}
s_bookmarks->removeLines(lineList);
}
else // should never occur as button (incl. shortcut) gets disabled
m_stline->showError("bookmarks", "No bookmarks selected for removal");
}
// ----------------------------------------------------------------------------
// Static state & interface
DlgBookmarks * DlgBookmarks::s_instance;
QByteArray DlgBookmarks::s_winGeometry;
QByteArray DlgBookmarks::s_winState;
MainWin * DlgBookmarks::s_mainWin;
MainText * DlgBookmarks::s_mainText;
MainSearch * DlgBookmarks::s_search;
Highlighter * DlgBookmarks::s_higl;
Bookmarks * DlgBookmarks::s_bookmarks;
/**
* This function is called when writing the config file to retrieve persistent
* settings of this dialog. Currently this includes the dialog geometry and
* tool-bar state and the color palettes. Note the highlight definitions are
* not included here, as the dialog class only holds a temporary copy while
* open.
*/
QJsonObject DlgBookmarks::getRcValues() /*static*/
{
QJsonObject obj;
if (s_instance)
{
s_winGeometry = s_instance->saveGeometry();
s_winState = s_instance->saveState();
}
obj.insert("win_geom", QJsonValue(QString(s_winGeometry.toHex())));
obj.insert("win_state", QJsonValue(QString(s_winState.toHex())));
return obj;
}
/**
* This function is called during start-up to apply configuration variables.
* The values are simply stored and applied when the dialog is actually opened.
*/
void DlgBookmarks::setRcValues(const QJsonValue& val) /*static*/
{
const QJsonObject obj = val.toObject();
for (auto it = obj.begin(); it != obj.end(); ++it)
{
const QString& var = it.key();
const QJsonValue& val = it.value();
if (var == "win_geom")
{
s_winGeometry = QByteArray::fromHex(val.toString().toLatin1());
}
else if (var == "win_state")
{
s_winState = QByteArray::fromHex(val.toString().toLatin1());
}
}
}
/**
* This static external interface function creates and shows the dialog window.
* If the window is already open, it is only raised. There can only be one
* instance of the dialog.
*/
void DlgBookmarks::openDialog() /*static*/
{
if (s_instance == nullptr)
{
s_instance = new DlgBookmarks();
}
else
{
s_instance->activateWindow();
s_instance->raise();
}
}
/**
* This external interface function is called once during start-up after all
* classes are instantiated to establish the required connections.
*/
void DlgBookmarks::connectWidgets(MainWin * mainWin, MainSearch * search, MainText * mainText,
Highlighter * higl, Bookmarks * bookmarks) /*static*/
{
s_mainWin = mainWin;
s_mainText = mainText;
s_search = search;
s_higl = higl;
s_bookmarks = bookmarks;
}
DlgBookmarks* DlgBookmarks::getInstance()
{
openDialog();
return s_instance;
}