Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fb2note.cpp « source - github.com/lintest/fb2edit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c6efa771af901828f67c6b3301d1de6d5dde7d4 (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
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
#include "fb2note.hpp"

#include <QComboBox>
#include <QDialogButtonBox>
#include <QHeaderView>
#include <QLabel>
#include <QLineEdit>
#include <QSplitter>
#include <QToolBar>
#include <QWebFrame>
#include <QWebPage>
#include <QWebView>

#include "fb2list.hpp"
#include "fb2page.hpp"
#include "fb2text.hpp"
#include "fb2html.h"

//---------------------------------------------------------------------------
//  FbNoteDlg
//---------------------------------------------------------------------------

FbNoteDlg::FbNoteDlg(FbTextBase *text)
    : QDialog(text)
{
    setWindowTitle(tr("Insert footnote"));
    resize(460, 300);

    QGridLayout * gridLayout = new QGridLayout(this);

    QLabel * label1 = new QLabel(this);
    label1->setText(tr("Identifier:"));
    gridLayout->addWidget(label1, 0, 0, 1, 1);

    m_key = new QComboBox(this);
    QSizePolicy sizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    gridLayout->addWidget(m_key, 0, 1, 1, 1);

    QLabel * label2 = new QLabel(this);
    label2->setText(tr("Title:"));
    gridLayout->addWidget(label2, 1, 0, 1, 1);

    m_title = new QLineEdit(this);
    m_title->setObjectName(QString::fromUtf8("m_title"));
    gridLayout->addWidget(m_title, 1, 1, 1, 1);

    m_toolbar = new QToolBar(this);
    m_toolbar->setIconSize(QSize(24, 24));
    gridLayout->addWidget(m_toolbar, 2, 0, 1, 2);

    QFrame * frame = new QFrame(this);
    frame->setFrameShape(QFrame::StyledPanel);
    frame->setFrameShadow(QFrame::Sunken);
    gridLayout->addWidget(frame, 3, 0, 1, 2);

    QLayout * frameLayout = new QHBoxLayout(frame);
    frameLayout->setSpacing(0);
    frameLayout->setMargin(0);

    m_text = new FbTextBase(frame);
    m_text->setObjectName(QString::fromUtf8("m_text"));
    m_text->setUrl(QUrl(QString::fromUtf8("about:blank")));
    frameLayout->addWidget(m_text);

    QDialogButtonBox * buttonBox = new QDialogButtonBox(this);
    buttonBox->setObjectName(QString::fromUtf8("buttonBox"));
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    gridLayout->addWidget(buttonBox, 4, 0, 1, 2);

    QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    m_key->addItem(tr("<create new>"));
    m_key->setCurrentIndex(0);
    m_title->setFocus();

    FbTextPage *page = new FbTextPage(this);
    connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
    page->setNetworkAccessManager(text->page()->networkAccessManager());
    page->setContentEditable(true);
    m_text->setPage(page);
    m_text->setHtml("<body><p><br></p></body>");

    m_text->addTools(m_toolbar);
}

void FbNoteDlg::loadFinished()
{
    FbTextElement body = m_text->page()->mainFrame()->documentElement().findFirst("body");
    body.select();
}

//---------------------------------------------------------------------------
//  FbNotesModel
//---------------------------------------------------------------------------

FbNotesModel::FbNotesModel(FbTextPage *page, QObject *parent)
    : QAbstractListModel(parent)
    , collection(page->body().findAll("a"))
    , m_page(page)
{
}

FbTextElement FbNotesModel::at(const QModelIndex &index) const
{
    int row = index.row();
    if (row < 0 || row >= collection.count()) return QWebElement();
    return collection.at(row);
}

int FbNotesModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 4;
}

int FbNotesModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : collection.count();
}

QVariant FbNotesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 1: return tr("Title");
            case 2: return tr("Type");
            case 3: return tr("Link");
        }
    }
    return QVariant();
}

QVariant FbNotesModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        switch (role) {
            case Qt::DisplayRole: {
                QWebElement element = at(index);
                switch (index.column()) {
                    case 1: return element.toPlainText();
                    case 2: return element.attribute("type");
                    default: return element.attribute("href");
                }
            } break;
            case Qt::TextAlignmentRole: {
                return Qt::AlignLeft;
            }
        }
    }
    return QVariant();
}

//---------------------------------------------------------------------------
//  FbNotesWidget
//---------------------------------------------------------------------------

FbNotesWidget::FbNotesWidget(FbTextEdit *text, QWidget* parent)
    : QWidget(parent)
    , m_text(text)
{
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);

    QSplitter *splitter = new QSplitter(Qt::Vertical, this);

    m_list = new FbListView(splitter);
    m_list->header()->setDefaultSectionSize(50);
    splitter->addWidget(m_list);

    FbTextFrame *frame = new FbTextFrame(splitter);
    splitter->addWidget(frame);

    m_view = new FbTextBase(frame);
    m_view->page()->setNetworkAccessManager(text->page()->networkAccessManager());
    m_view->page()->settings()->setUserStyleSheetUrl(QUrl::fromLocalFile(":style.css"));
    frame->layout()->addWidget(m_view);

    splitter->setSizes(QList<int>() << 100 << 100);

    layout->addWidget(splitter);

    connect(m_text, SIGNAL(loadFinished(bool)), SLOT(loadFinished()));
    connect(m_list, SIGNAL(showCurrent(QString)), SLOT(showCurrent(QString)));
    connect(m_list, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
    loadFinished();
}

void FbNotesWidget::activated(const QModelIndex &index)
{
    if (FbNotesModel *m = qobject_cast<FbNotesModel*>(m_list->model())) {
        m->at(index).select();
    }
}

void FbNotesWidget::loadFinished()
{
    if (QAbstractItemModel *m = m_list->model()) m->deleteLater();
    m_view->load(QUrl());
    m_list->setModel(new FbNotesModel(m_text->page(), this));
    m_list->reset();
    m_list->setColumnHidden(0, true);
}

void FbNotesWidget::showCurrent(const QString &name)
{
    QWebElement element = m_text->body().findFirst(name);
    QString html = element.toInnerXml();
    html.prepend(
        "<fb:body name=notes style='padding:0;margin:0;'>"
        "<fb:section id=0 style='border:0;padding:0;margin:0;'>"
    );
    html.append("</fb:section></fb:body>");
    m_view->setHtml(html, m_view->url());
}