-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandbarwidget.cpp
More file actions
121 lines (102 loc) · 3.89 KB
/
Copy pathcommandbarwidget.cpp
File metadata and controls
121 lines (102 loc) · 3.89 KB
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
#include "commandbarwidget.h"
#include "utils.h"
#include <QFileDialog>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QPushButton>
#include <regex>
CommandBarWidget::CommandBarWidget(QWidget *parent)
: QWidget{parent}
{
QWidget *cmdBarWidget = new QWidget();
// Command bar items
QPushButton *fileBtn = new QPushButton("File");
fileBtn->setToolTip("Open a file");
fileBtn->setFixedSize(40, 20);
QObject::connect(fileBtn,
&QAbstractButton::clicked,
this,
&CommandBarWidget::handleFileBtnClick);
QPushButton *urlBtn = new QPushButton("URL");
urlBtn->setToolTip("Open a URL");
urlBtn->setFixedSize(40, 20);
QObject::connect(urlBtn, &QAbstractButton::clicked, this, &CommandBarWidget::handleUrlBtnClick);
selectedLabel = new QLabel("");
// Allow label to get cut off if it's too long
selectedLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
selectedLabel->setMinimumWidth(1);
// Command bar layout
QHBoxLayout *cmdBarLayout = new QHBoxLayout(cmdBarWidget);
cmdBarLayout->setContentsMargins(10, 0, 10, 0);
cmdBarLayout->addWidget(fileBtn);
cmdBarLayout->addWidget(urlBtn);
cmdBarLayout->addWidget(selectedLabel);
setLayout(cmdBarLayout);
}
void CommandBarWidget::connectWebView(DNDWebEngineView *webView)
{
QObject::connect(webView,
&DNDWebEngineView::updateCommandStatusText,
this,
&CommandBarWidget::setSelectedLabelText);
}
void CommandBarWidget::handleFileBtnClick()
{
QString dirToOpen;
if (!lastUsedDir.isEmpty())
dirToOpen = lastUsedDir;
else
dirToOpen = QDir::homePath();
QString fileName = QFileDialog::getOpenFileName(this, "Change file", dirToOpen);
if (!fileName.isEmpty()) {
if (!Utils::isUnsupportedFile(fileName)) {
lastUsedDir = fileName;
fileName.insert(0, "file://");
setSelectedLabelText(fileName);
emit newFileSelected(fileName);
emit clearStatus();
} else {
emit pushStatus(QString::fromStdString("Unsupported filetype: "
+ fileName.toStdString().substr(
fileName.toStdString().find_last_of("."))),
QString("Error"));
}
}
}
void CommandBarWidget::handleUrlBtnClick()
{
bool ok;
QString qUrl = QInputDialog::getText(this, // Parent widget
"Change URL", // Dialog title
"Enter URL", // Label text
QLineEdit::Normal, // Echo mode
QString(), // Default text
&ok); // Pointer to a bool to check if OK was clicked
if (ok && !qUrl.isEmpty()) {
std::string url = qUrl.toStdString();
// If there is no protocol indicator, prepend https:// and hope for the best!
if (url.find("://") == std::string::npos)
url.insert(0, "https://");
if (isValidUrl(url)) {
qUrl = QString::fromStdString(url);
setSelectedLabelText(qUrl);
emit newUrlSelected(qUrl);
emit clearStatus();
} else {
qDebug() << "Invalid url" << QString::fromStdString(url);
}
}
}
void CommandBarWidget::setSelectedLabelText(const QString &newFileOrUrl)
{
selectedLabel->setText(newFileOrUrl);
selectedLabel->setToolTip(newFileOrUrl);
}
bool CommandBarWidget::isValidUrl(std::string url)
{
const std::regex pattern(
"https?:\\/\\/"
"(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,4}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)");
return std::regex_match(url, pattern);
}