summaryrefslogtreecommitdiff
path: root/src/frontend/keychooser/bthistory.cpp
blob: 8b14ded8c0fda2775df9bbac5ef2a83fa2b3fd93 (plain)
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
//
// C++ Implementation: BTHistory
//
// Description:
//
//
// Author: The BibleTime team <info@bibletime.info>, (C) 2007
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include "bthistory.h"

#include "backend/keys/cswordkey.h"


#include <QAction>
#include <QList>

#include <QDebug>


BTHistory::BTHistory(QWidget* parent)
        : m_historyList(),
        m_index(-1),
        m_inHistoryFunction(false) {
    setParent(parent);
    Q_ASSERT(class_invariant());
}

void BTHistory::add(CSwordKey* newKey) {
    qDebug("BTHistory::add");
    Q_ASSERT(newKey);
    // Add new key Action after current index if we were not using the history functions,
    // if it's not a duplicate and if it's not empty.
    if (!m_inHistoryFunction &&	((m_index < 0) || (newKey->key() != m_historyList.at(m_index)->text()) )) {
        if (!newKey->key().isEmpty()) {
            m_historyList.insert(++m_index, new QAction(newKey->key(), this));
        }
        // TODO: history limit?
        sendChangedSignal();
    }
    Q_ASSERT(class_invariant());
}

void BTHistory::move(QAction* historyItem) {
    qDebug("BTHistory::move");
    //Q_ASSERT(historyItem);
    Q_ASSERT(m_historyList.count());

    m_inHistoryFunction = true;
    //find the action in the list
    m_index = m_historyList.indexOf(historyItem);
    //move to the selected item in the list, it will be the current item
    QString newKey = m_historyList.at(m_index)->text();
    emit historyMoved(newKey); // signal to "outsiders"; key has been changed
    sendChangedSignal();

    m_inHistoryFunction = false;
    Q_ASSERT(class_invariant());
}

void BTHistory::back() {
    qDebug("BTHistory::back");
    if ( m_index >= 1) {
        move(m_historyList.at(m_index - 1));
    }
    Q_ASSERT(class_invariant());
}

void BTHistory::fw() {
    qDebug("BTHistory::fw");
    if (m_index < (m_historyList.size() - 1)) {
        move(m_historyList.at(m_index + 1));
    }
    Q_ASSERT(class_invariant());
}

QList<QAction*> BTHistory::getBackList() {
    qDebug("BTHistory::getBackList");

    QList<QAction*> list;
    for (int i = m_index - 1; i >= 0; --i) {
        list.append(m_historyList.at(i));
    }

    qDebug() << "return:" << list;
    Q_ASSERT(class_invariant());
    return list;
}

QList<QAction*> BTHistory::getFwList() {
    qDebug("BTHistory::getFwList");

    QList<QAction*> list;
    //qDebug() << "historyList.size:" << m_historyList.size();
    for (int i = m_index + 1; i < m_historyList.size(); ++i) {
        //qDebug() << "i:" << i;
        list.append(m_historyList.at(i));
    }
    qDebug() << "return:" << list;

    Q_ASSERT(class_invariant());
    return list;
}

void BTHistory::sendChangedSignal() {
    bool backEnabled = m_index > 0; //there are items in the back list
    bool fwEnabled = m_historyList.size() > m_index + 1; //there are items in the fw list
    emit historyChanged(backEnabled, fwEnabled);
    Q_ASSERT(class_invariant());
}

bool BTHistory::class_invariant() {
    for (int i = 0; i < m_historyList.size(); ++i) {
        if (!m_historyList.at(i) || m_historyList.at(i)->text().isEmpty()) return false;
    }
    if (!(m_index >= -1 && m_index < m_historyList.size())) return false;
    return true;
}