-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
236 lines (212 loc) · 6.75 KB
/
mainwindow.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <QFileDialog>
#include <QMimeData>
#include <QMimeDatabase>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <QProcess>
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setAcceptDrops(true);
dw = new DrawnWidget(this);
ui->pichost->layout()->addWidget(dw);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::dragEnterEvent(QDragEnterEvent *evt)
{
if (evt->mimeData()->hasUrls()) {
evt->accept();
} else {
evt->ignore();
}
}
void MainWindow::dropEvent(QDropEvent *evt)
{
auto urls = evt->mimeData()->urls();
foreach(QUrl url, urls) {
qsl.append(url.toLocalFile());
ui->listWidget->addItem(url.toLocalFile());
}
}
void MainWindow::on_listWidget_currentTextChanged(const QString ¤tText)
{
pm.load(currentText);
ui->width->setText(QString::number(pm.width()));
ui->height->setText(QString::number(pm.height()));
dw->setPixmap(pm);
dw->update();
}
DrawnWidget::DrawnWidget(QWidget *parent) : QWidget(parent) {
this->setMinimumSize(128,128);
}
void DrawnWidget::setPixmap(QPixmap &pm)
{
this->pm = pm;
}
void DrawnWidget::paintEvent(QPaintEvent *evt)
{
Q_UNUSED(evt);
QPainter painter(this);
int pw, ph;
// draw background
QPalette p = this->palette();
painter.fillRect(QRect(0,0,width(),height()), QColor(0xffffff));
painter.setPen(p.light().color());
painter.setBrush(p.base());
painter.drawRect(QRect(0,0,width()-1,height()-1));
// do nothing if there's no pixmap loaded
if (pm.isNull()) {
return;
}
// calculate image area
pw = pm.width();
ph = pm.height();
int aw = width() - 2; // minus the 1px border
int ah = height() - 2;
int x, y, w, h;
float ratio = (float)pw / (float)ph;
// fit along most maximal dimension first,
// then fallback to minimal fit if cropped
auto fitWidth = [&]() {
w = aw;
h = w / ratio;
x = 0;
y = (ah - h)/2;
};
auto fitHeight = [&]() {
h = ah;
w = h * ratio;
x = (aw - w)/2;
y = 0;
};
if (ratio <= 1.0) { // height is greater than width
fitHeight();
if (x < 0)
fitWidth();
} else { // width is greater than height
fitWidth();
if (y < 0)
fitHeight();
}
// draw pixmap
QRect r(QPoint(x+1,y+1),QSize(w,h));
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.drawPixmap(r,pm);
}
void MainWindow::on_importButton_clicked()
{
QFileDialog *qfd = new QFileDialog(this);
qfd->setAttribute(Qt::WA_DeleteOnClose);
qfd->setWindowTitle(tr("Import File"));
connect(qfd, &QFileDialog::filesSelected, [this](QStringList items) {
this->ui->listWidget->clear();
this->qsl.clear();
// foreach file, read all its items and add to working lists
foreach (QString fname, items) {
QFile f(fname);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
else {
QTextStream in(&f);
QString line;
while (true) {
line = in.readLine();
if (line.isNull())
break;
this->ui->listWidget->addItem(QString(line));
this->qsl.append(QString(line));
}
}
}
});
qfd->show();
}
void MainWindow::on_exportButton_clicked()
{
QFileDialog *qfd = new QFileDialog(this);
qfd->setAttribute(Qt::WA_DeleteOnClose);
qfd->setWindowTitle("Export File");
connect(qfd, &QFileDialog::fileSelected, [this](QString item) {
if (item.isEmpty())
return;
QFile f(item);
if (!f.open(QIODevice::WriteOnly | QIODevice::Text))
return;
QTextStream out(&f);
foreach(QString s, qsl)
out << s << '\n';
});
qfd->show();
}
void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
QMenu m(this);
QAction *a = new QAction(&m);
a->setText("Remove");
connect(a, &QAction::triggered, [this]() {
int index = this->ui->listWidget->currentRow();
if (index < 0)
return;
this->ui->listWidget->takeItem(index);
this->qsl.takeAt(index);
});
qDebug() << ui->listWidget->mapToGlobal(pos);
m.addAction(a);
QMenu *openwith = m.addMenu("Open With");
QString file = qsl.value(this->ui->listWidget->currentRow());
QList<MimeTypeHandler *> apps = mimeTypeDb.appsFor(QMimeDatabase().mimeTypeForFile(file).name());
foreach (MimeTypeHandler *app, apps) {
a = new QAction(openwith);
a->setText(app->name);
connect(a, &QAction::triggered, [this, app, file]() {
if (file.isNull())
return;
QString cmd = app->exec;
if (cmd.contains(QRegExp("%[Uu]"))) {
QString url = QString::fromLatin1(QUrl::fromLocalFile(file).toEncoded());
cmd.replace(QRegExp("%[Uu]"), QString("\"%1\"").arg(url));
}
if (cmd.contains(QRegExp("%[Ff]")))
cmd.replace(QRegExp("%[Ff]"), QString("\"%1\"").arg(file));
QProcess *p = new QProcess();
p->start(cmd);
connect(p, static_cast<void (QProcess::*)(int)>(&QProcess::finished),
[p](int){ p->deleteLater(); });
});
openwith->addAction(a);
}
m.exec(ui->listWidget->mapToGlobal(pos));
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this, "About File Lister",
"<h2>File Lister</h2>"
"<p>A simple way to make file lists"
"<p>Based on Qt " QT_VERSION_STR
"<p>Built on " __DATE__ " at " __TIME__
"<h3>LICENSE</h3>"
"<p> Copyright (C) 2015"
"<p>"
"This program is free software; you can redistribute it and/or modify "
"it under the terms of the GNU General Public License as published by "
"the Free Software Foundation; either version 2 of the License, or "
"(at your option) any later version."
"<p>"
"This program is distributed in the hope that it will be useful, "
"but WITHOUT ANY WARRANTY; without even the implied warranty of "
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the "
"GNU General Public License for more details."
"<p>"
"You should have received a copy of the GNU General Public License "
"along with this program; if not, write to the Free Software "
"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA "
"02110-1301 USA.");
}