-
Notifications
You must be signed in to change notification settings - Fork 0
/
Widget.cpp
124 lines (102 loc) · 3.87 KB
/
Widget.cpp
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
#include "Widget.h"
#include "WikiDocument.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QTextBrowser>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QListView>
#include <QSortFilterProxyModel>
#include <QStandardItemModel>
#include <QDir>
#include <QSettings>
Widget::Widget(QWidget *parent)
: QWidget(parent),
m_document(new WikiDocument(this))
{
QHBoxLayout *mainLayout = new QHBoxLayout;
setLayout(mainLayout);
QVBoxLayout *leftLayout = new QVBoxLayout;
mainLayout->addLayout(leftLayout);
QHBoxLayout *navigationLayout = new QHBoxLayout;
leftLayout->addLayout(navigationLayout);
QPushButton *backButton = new QPushButton("Back");
navigationLayout->addWidget(backButton);
backButton->setEnabled(false);
QPushButton *forwardButton = new QPushButton("Forward");
forwardButton->setEnabled(false);
navigationLayout->addWidget(forwardButton);
QLineEdit *pagesFilterEdit = new QLineEdit;
pagesFilterEdit->setPlaceholderText("Search...");
leftLayout->addWidget(pagesFilterEdit);
QListView *pageListView = new QListView;
leftLayout->addWidget(pageListView);
QStandardItemModel *pagesModel = new QStandardItemModel(this);
for (const QString &fileName : QDir(":/pages").entryList({"*.txt"}, QDir::Files)) {
QFile file(":/pages/" + fileName);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "failed to open" << fileName;
continue;
}
QString title = QString::fromUtf8(file.readLine().trimmed()).remove("title:");
if (title.isEmpty()) {
title = fileName;
title.remove(".txt");
}
QStandardItem *item = new QStandardItem(title);
item->setData(fileName, Qt::UserRole);
pagesModel->appendRow(item);
}
QSortFilterProxyModel *searchModel = new QSortFilterProxyModel(this);
searchModel->setSourceModel(pagesModel);
pageListView->setModel(searchModel);
pageListView->setMaximumWidth(pageListView->sizeHintForColumn(0));
pagesFilterEdit->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
m_document->setDocumentMargin(20);
m_browser = new WikiBrowser(this);
m_browser->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
m_browser->setDocument(m_document);
layout()->addWidget(m_browser);
layout()->setMargin(0);
// m_browser->setOpenLinks(false);
connect(m_browser, &QTextBrowser::historyChanged, this, []() {
qWarning() << "history changed";
});
connect(m_browser, &QTextBrowser::sourceChanged, this, [](const QUrl &src) {
qWarning() << "source changed" << src;
});
connect(m_browser, &QTextBrowser::backwardAvailable, backButton, &Widget::setEnabled);
connect(m_browser, &QTextBrowser::forwardAvailable, forwardButton, &Widget::setEnabled);
connect(backButton, &QPushButton::clicked, m_browser, &QTextBrowser::backward);
connect(forwardButton, &QPushButton::clicked, m_browser, &QTextBrowser::forward);
connect(pageListView, &QListView::clicked, m_browser, [=](const QModelIndex &index) {
m_browser->setSource("qrc:///pages/" + index.data(Qt::UserRole).toString());
});
m_browser->setSource(QString("qrc:///pages/members-pages.txt"));
connect(pagesFilterEdit, &QLineEdit::textChanged, searchModel, &QSortFilterProxyModel::setFilterFixedString);
QSettings settings;
QSize lastSize = settings.value("windowsize").toSize();
if (lastSize.isEmpty()) {
resize(1024, 768);
} else {
resize(lastSize);
}
}
Widget::~Widget()
{
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton) {
return;
}
qDebug() << "click at" << event->pos();
}
void Widget::closeEvent(QCloseEvent *)
{
QSettings settings;
settings.setValue("windowsize", size());
}