BibleTime
src
frontend
keychooser
bthistory.cpp
Go to the documentation of this file.
1
/*********
2
*
3
* In the name of the Father, and of the Son, and of the Holy Spirit.
4
*
5
* This file is part of BibleTime's source code, https://bibletime.info/
6
*
7
* Copyright 1999-2021 by the BibleTime developers.
8
* The BibleTime source code is licensed under the GNU General Public License
9
* version 2.0.
10
*
11
**********/
12
13
#include "
bthistory.h
"
14
15
#include <QAction>
16
#include <QVariant>
17
#include "../../backend/keys/cswordkey.h"
18
#include "../../util/btassert.h"
19
20
21
namespace
{
22
char
const
ActionText
[] =
"BtHistory key"
;
23
}
24
25
BTHistory::BTHistory
(
QObject
*
const
parent)
26
:
QObject
(parent)
27
{
BT_ASSERT
(
class_invariant
()); }
28
29
void
BTHistory::add
(
CSwordKey
* newKey) {
30
BT_ASSERT
(newKey);
31
// Add new key Action after current index if we were not using the history functions,
32
// if it's not a duplicate and if it's not empty.
33
if
(!
m_inHistoryFunction
&& ((
m_index
< 0) || (newKey->
key
() !=
m_historyList
.at(
m_index
)->property(
ActionText
).toString()) )) {
34
if
(!newKey->
key
().isEmpty()) {
35
auto
*
const
a =
new
QAction
(newKey->
key
(),
this
);
36
a->setProperty(
ActionText
, newKey->
key
());
37
m_historyList
.insert(++
m_index
, a);
38
}
39
// \todo history limit?
40
sendChangedSignal
();
41
}
42
BT_ASSERT
(
class_invariant
());
43
}
44
45
void
BTHistory::move
(
QAction
* historyItem) {
46
//BT_ASSERT(historyItem);
47
BT_ASSERT
(
m_historyList
.count());
48
49
m_inHistoryFunction
=
true
;
50
//find the action in the list
51
m_index
=
m_historyList
.indexOf(historyItem);
52
//move to the selected item in the list, it will be the current item
53
Q_EMIT
historyMoved
(
m_historyList
.at(
m_index
)->property(
ActionText
).toString());
// signal to "outsiders"; key has been changed
54
sendChangedSignal
();
55
56
m_inHistoryFunction
=
false
;
57
BT_ASSERT
(
class_invariant
());
58
}
59
60
void
BTHistory::back
() {
61
if
(
m_index
>= 1) {
62
move
(
m_historyList
.at(
m_index
- 1));
63
}
64
BT_ASSERT
(
class_invariant
());
65
}
66
67
void
BTHistory::fw
() {
68
if
(
m_index
< (
m_historyList
.size() - 1)) {
69
move
(
m_historyList
.at(
m_index
+ 1));
70
}
71
BT_ASSERT
(
class_invariant
());
72
}
73
74
QList<QAction*>
BTHistory::getBackList
() {
75
76
QList<QAction*>
list;
77
for
(
int
i =
m_index
- 1; i >= 0; --i) {
78
list.append(
m_historyList
.at(i));
79
}
80
81
BT_ASSERT
(
class_invariant
());
82
return
list;
83
}
84
85
QList<QAction*>
BTHistory::getFwList
() {
86
QList<QAction*>
list;
87
for
(
int
i =
m_index
+ 1; i <
m_historyList
.size(); ++i) {
88
list.append(
m_historyList
.at(i));
89
}
90
91
BT_ASSERT
(
class_invariant
());
92
return
list;
93
}
94
95
void
BTHistory::sendChangedSignal
() {
96
bool
backEnabled =
m_index
> 0;
//there are items in the back list
97
bool
fwEnabled =
m_historyList
.size() >
m_index
+ 1;
//there are items in the fw list
98
Q_EMIT
historyChanged
(backEnabled, fwEnabled);
99
BT_ASSERT
(
class_invariant
());
100
}
101
102
bool
BTHistory::class_invariant
() {
103
for
(
int
i = 0; i <
m_historyList
.size(); ++i) {
104
if
(!
m_historyList
.at(i) ||
m_historyList
.at(i)->property(
ActionText
).toString().isEmpty())
return
false
;
105
}
106
if
(!(
m_index
>= -1 &&
m_index
<
m_historyList
.size()))
return
false
;
107
return
true
;
108
}
BT_ASSERT
#define BT_ASSERT(...)
Definition:
btassert.h:17
bthistory.h
BTHistory::m_historyList
QList< QAction * > m_historyList
Definition:
bthistory.h:72
BTHistory::move
void move(QAction *)
Definition:
bthistory.cpp:45
BTHistory::BTHistory
BTHistory(QObject *parent)
Definition:
bthistory.cpp:25
BTHistory::historyMoved
void historyMoved(QString newKey)
BTHistory::fw
void fw()
Definition:
bthistory.cpp:67
BTHistory::getFwList
QList< QAction * > getFwList()
Definition:
bthistory.cpp:85
BTHistory::class_invariant
bool class_invariant()
Definition:
bthistory.cpp:102
BTHistory::sendChangedSignal
void sendChangedSignal()
Definition:
bthistory.cpp:95
BTHistory::getBackList
QList< QAction * > getBackList()
Definition:
bthistory.cpp:74
BTHistory::back
void back()
Definition:
bthistory.cpp:60
BTHistory::m_inHistoryFunction
bool m_inHistoryFunction
Definition:
bthistory.h:74
BTHistory::add
void add(CSwordKey *newKey)
Definition:
bthistory.cpp:29
BTHistory::historyChanged
void historyChanged(bool backEnabled, bool fwEnabled)
BTHistory::m_index
int m_index
Definition:
bthistory.h:73
CSwordKey
Definition:
cswordkey.h:22
CSwordKey::key
virtual QString key() const =0
QAction
QList
QObject
anonymous_namespace{bthistory.cpp}::ActionText
char const ActionText[]
Definition:
bthistory.cpp:22
Generated by
1.9.1