-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuestionMarkFinder.cpp
129 lines (110 loc) · 4.02 KB
/
QuestionMarkFinder.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
125
126
127
128
#include "QuestionMarkFinder.h"
#include <QLayout>
#include <QSplitter>
#include <QFileDialog>
#include <QDebug>
#include <QStringListModel>
#include <QString>
#include <QByteArray>
#include "DocumentWidget.h"
#include <QShortcut>
#include <QInputDialog>
#include <Global.h>
#include <QListView>
#include <QTreeView>
#include <QFileSystemModel>
#include <QSyntaxHighlighter>
QuestionMarkFinder::QuestionMarkFinder(QWidget *parent)
: QWidget(parent)
, mOpenButton (new QPushButton(QIcon(":/openInBrowser.png"),""))
, mOpenFolderButton (new QPushButton(QIcon(":/openFolder.png"),""))
, mTabs (new QTabWidget())
{
setup();
}
void QuestionMarkFinder::setup() {
setupGui();
setupShortcuts();
}
void QuestionMarkFinder::setupGui() {
QVBoxLayout* mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(createToolButtons());
mainLayout->addWidget(createTabWidget());
// mainLayout->addWidget(createFileSystemView());
mainLayout->setMargin(0);
setupTabBehavior();
}
void QuestionMarkFinder::setupShortcuts() {
QShortcut* openShortcut = new QShortcut(QKeySequence("Ctrl+o"), this);
QShortcut* openIfExistShortcut = new QShortcut(QKeySequence("Ctrl+Shift+o"), this);
connect(openShortcut, &QShortcut::activated, this, &QuestionMarkFinder::openDocuments);
connect(openIfExistShortcut, &QShortcut::activated, this, &QuestionMarkFinder::openIfContain);
}
void QuestionMarkFinder::setupTabBehavior() {
mTabs->setTabsClosable(true);
connect(mTabs, &QTabWidget::tabCloseRequested, [this](int index) { mTabs->removeTab(index);} );
connect(mTabs, &QTabWidget::tabCloseRequested, this, &QuestionMarkFinder::addDefaultTab );
}
void QuestionMarkFinder::openDocuments() {
DocumentWidgets documents = load();
for (auto doc : documents) mTabs->addTab(doc, doc->name());
}
void QuestionMarkFinder::openIfContain() {
bool ok;
QInputDialog* inputWidget = new QInputDialog();
this->layout()->addWidget(inputWidget);
QString neededText = inputWidget->getText(this, tr("Smart open")
,tr("Open files that containing this substring:"), QLineEdit::Normal,"your text", &ok);
if (ok && !neededText.isEmpty()) {
auto documents = load();
for (auto doc : documents) {
int lastIndex = doc->findText(neededText);
qDebug() << lastIndex;
if (lastIndex != Search::NotFound) {
mTabs->addTab(doc, doc->name());
}
else delete doc;
}
}
}
QuestionMarkFinder::DocumentWidgets QuestionMarkFinder::load() {
QStringList fileNames = QFileDialog::getOpenFileNames(this, "", "", "HTML files (*.html *.htm)");
DocumentWidgets result;
for (const QString& fileName : fileNames) {
result.push_back(new DocumentWidget(fileName, this));
}
return result;
}
QWidget* QuestionMarkFinder::createToolButtons(){
QWidget* widget = new QWidget(this);
QHBoxLayout* layout = new QHBoxLayout(widget);
layout->addWidget(mOpenButton);
layout->addWidget(mOpenFolderButton);
layout->addSpacerItem(new QSpacerItem(10, 10, QSizePolicy::Expanding, QSizePolicy::Minimum));
return widget;
}
QWidget* QuestionMarkFinder::createTabWidget() {
QWidget* widget = new QWidget(this);
QVBoxLayout* vBoxLayout = new QVBoxLayout(widget);
vBoxLayout->addWidget(mTabs);
addDefaultTab();
vBoxLayout->setMargin(0);
return widget;
}
QWidget* QuestionMarkFinder::createFileSystemView() {
QListView* view = new QListView(this);
QFileSystemModel* model = new QFileSystemModel();
QStringList filters;
filters << "*.html" << "*.htm";
model->setRootPath(QDir::currentPath());
model->setNameFilterDisables(false);
model->setNameFilters(filters);
view->setModel(model);
view->setRootIndex(model->index(QDir::currentPath()));
connect(view, &QListView::doubleClicked, [view] (QModelIndex index) { view->setRootIndex(index); } );
//connect(view, &QListView::, [view] (QModelIndex index) { view->setRootIndex(index);} );
return view;
}
void QuestionMarkFinder::addDefaultTab() {
if (mTabs->count() < 1) mTabs->addTab(new DocumentWidget(this), "blank:info");
}