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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
//===========================================
// Lumina-DE source code
// Copyright (c) 2014-2015, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "MainUI.h"
#include "ui_MainUI.h"
#include <LuminaX11.h>
#include <QMessageBox>
#include <QClipboard>
MainUI::MainUI()
: QMainWindow(), ui(new Ui::MainUI),
mousegrabbed(false),
picSaved(false),
closeOnSave(false)
{
ui->setupUi(this); //load the designer file
XCB = new LXCB();
IMG = new ImageEditor(this);
ui->scrollArea->setWidget(IMG);
areaOverlay = 0;
ui->label_zoom_percent->setMinimumWidth( ui->label_zoom_percent->fontMetrics().width("200%") );
setupIcons();
ui->spin_monitor->setMaximum(QApplication::desktop()->screenCount());
if(ui->spin_monitor->maximum()<2){
ui->spin_monitor->setEnabled(false);
ui->radio_monitor->setEnabled(false);
}
scaleTimer = new QTimer(this);
scaleTimer->setSingleShot(true);
scaleTimer->setInterval(200); //~1/5 second
tabbar = new QTabBar(this);
tabbar->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
ui->tabLayout->insertWidget(0,tabbar, Qt::AlignLeft | Qt::AlignBottom);
tabbar->addTab(LXDG::findIcon("view-preview",""), tr("View"));
tabbar->addTab(LXDG::findIcon("preferences-other",""), tr("Settings"));
ui->stackedWidget->setCurrentWidget(ui->page_current);
//Add a spacer in the Toolbar
QWidget *spacer = new QWidget(this);
spacer->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);
ui->toolBar->insertWidget(ui->actionClose, spacer);
//Setup the connections
//connect(ui->tool_save, SIGNAL(clicked()), this, SLOT(saveScreenshot()) );
connect(ui->actionSave_As, SIGNAL(triggered()), this, SLOT(saveScreenshot()) );
connect(ui->tool_quicksave, SIGNAL(clicked()), this, SLOT(quicksave()) );
connect(ui->actionQuick_Save, SIGNAL(triggered()), this, SLOT(quicksave()) );
connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(closeApplication()) );
//connect(ui->push_snap, SIGNAL(clicked()), this, SLOT(startScreenshot()) );
connect(ui->actionTake_Screenshot, SIGNAL(triggered()), this, SLOT(startScreenshot()) );
connect(ui->tool_crop, SIGNAL(clicked()), IMG, SLOT(cropImage()) );
connect(ui->tool_copy_to_clipboard, SIGNAL(clicked()), this, SLOT(copyToClipboard()) );
connect(IMG, SIGNAL(selectionChanged(bool)), this, SLOT(imgselchanged(bool)) );
connect(IMG, SIGNAL(scaleFactorChanged(int)), this, SLOT(imgScalingChanged(int)) );
connect(ui->slider_zoom, SIGNAL(valueChanged(int)), this, SLOT(sliderChanged()) );
connect(scaleTimer, SIGNAL(timeout()), this, SLOT(imgScalingChanged()) );
connect(tabbar, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)) );
connect(ui->check_show_popups, SIGNAL(toggled(bool)), this, SLOT(showPopupsChanged(bool)) );
settings = LUtils::openSettings("lumina-desktop", "lumina-screenshot",this);
ppath = settings->value("previous-path", QDir::homePath()).toString();
QString opt = settings->value("screenshot-target", "window").toString();
if( opt == "window") {ui->radio_window->setChecked(true); }
else if(opt=="area"){ ui->radio_area->setChecked(true); }
else{ ui->radio_all->setChecked(true); }
ui->spin_delay->setValue(settings->value("screenshot-delay", 0).toInt());
ui->check_show_popups->setChecked( settings->value("showPopupWarnings",true).toBool() );
ui->tool_resize->setVisible(false); //not implemented yet
this->show();
IMG->setDefaultSize(ui->scrollArea->maximumViewportSize());
IMG->LoadImage( QApplication::screens().at(0)->grabWindow(QApplication::desktop()->winId()).toImage() ); //initial screenshot
lastScreenShot = QDateTime::currentDateTime();
// Shortcuts
quitShortcut = new QShortcut(Qt::CTRL + Qt::Key_Q, this);
connect(quitShortcut, SIGNAL(activated()), this, SLOT(quitShortcut_activated()) );
openShortcut = new QShortcut(Qt::CTRL + Qt::Key_O, this);
connect(openShortcut, SIGNAL(activated()), this, SLOT(quicksave()) );
//ui->label_screenshot->setPixmap( cpic.scaled(ui->label_screenshot->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation) );
}
MainUI::~MainUI(){
if(areaOverlay!=0){ areaOverlay->deleteLater(); }
}
void MainUI::setupIcons(){
//Setup the icons
ui->tool_quicksave->setIcon( LXDG::findIcon("document-edit","") );
ui->actionSave_As->setIcon( LXDG::findIcon("document-save-as","") );
ui->actionQuick_Save->setIcon( LXDG::findIcon("document-save","") );
ui->actionClose->setIcon( LXDG::findIcon("application-exit","") );
ui->tool_copy_to_clipboard->setIcon( LXDG::findIcon("insert-image","") );
ui->actionTake_Screenshot->setIcon( LXDG::findIcon("camera-web","") );
ui->tool_crop->setIcon( LXDG::findIcon("transform-crop","") );
ui->tool_resize->setIcon( LXDG::findIcon("transform-scale","") );
this->setWindowIcon( LXDG::findIcon("camera-web","") );
}
void MainUI::showSaveError(QString path){
QMessageBox::warning(this, tr("Could not save screenshot"), tr("The screenshot could not be saved. Please check directory permissions or pick a different directory")+"\n\n"+path);
}
QRect MainUI::pointsToRect(QPoint pt1, QPoint pt2){
QRect rec;
if(pt1.x() < pt2.x()){ rec.setLeft(pt1.x()); rec.setRight(pt2.x()); }
else{ rec.setLeft(pt2.x()); rec.setRight(pt1.x()); }
if(pt1.y() < pt2.y()){ rec.setTop(pt1.y()); rec.setBottom(pt2.y()); }
else{ rec.setTop(pt2.y()); rec.setBottom(pt1.y()); }
return rec;
}
void MainUI::showSBMessage(QString msg, int secs){
//Show statusbar message (for designated time)
ui->statusBar->showMessage("----> "+msg, secs*1000);
}
//==============
// PRIVATE SLOTS
//==============
void MainUI::saveScreenshot(){
if(mousegrabbed){ return; }
QString filepath = QFileDialog::getSaveFileName(this, tr("Save Screenshot"), ppath+"/"+QString( "Screenshot-%1.png" ).arg( lastScreenShot.toString("yyyy-MM-dd-hh-mm-ss")), tr("PNG Files (*.png);;AllFiles (*)") );
if(filepath.isEmpty()){
closeOnSave = false;
return;
}
if(!filepath.endsWith(".png")){ filepath.append(".png"); }
if( !IMG->image().save(filepath, "png") ){
closeOnSave = false;
showSaveError(filepath);
}else{
picSaved = true;
ppath = filepath.section("/",0,-2); //just the directory
settings->setValue("previous-path", ppath);
if (closeOnSave) {
// We came here from close, now we need to close *after* handling
// the current screen event.
QTimer::singleShot(0, this, SLOT(close()));
}
}
}
void MainUI::quicksave(){
if(mousegrabbed){ return; }
QString savedir = QDir::homePath()+"/";
if(QFile::exists(savedir + "Pictures/")){ savedir.append("Pictures/"); }
else if(QFile::exists(savedir + "Images/")){ savedir.append("Images/"); }
QString path = savedir + QString( "Screenshot-%1.png" ).arg( lastScreenShot.toString("yyyy-MM-dd-hh-mm-ss") );
if(IMG->image().save(path, "png") ){
picSaved = true;
QProcess::startDetached("lumina-open -select \""+path+"\"");
}else{
showSaveError(path);
}
}
void MainUI::copyToClipboard(){
//qDebug() << "Copy Image to clipboard";
QClipboard *clipboard = QApplication::clipboard();
clipboard->setImage(IMG->image());
showSBMessage(tr("Image copied to clipboard"), 10);
//qDebug() << " - Success:" << !clipboard->image().isNull();
}
void MainUI::startScreenshot(){
if(mousegrabbed){ return; }
lastgeom = this->geometry();
if( !getWindow() ){ return; }
this->hide();
QTimer::singleShot(50+ui->spin_delay->value()*1000, this, SLOT(getPixmap()));
}
void MainUI::imgselchanged(bool hassel){
ui->tool_crop->setEnabled(hassel);
ui->tool_resize->setEnabled(hassel);
}
void MainUI::imgScalingChanged(int percent){
//qDebug() << "Scale Changed:" << percent;
if(percent<0){
//Changed by user interaction
IMG->setScaling(ui->slider_zoom->value());
}else{
ui->slider_zoom->setValue(percent);
}
ui->label_zoom_percent->setText( QString::number(ui->slider_zoom->value())+"%");
}
void MainUI::sliderChanged(){
ui->label_zoom_percent->setText( QString::number(ui->slider_zoom->value())+"%");
scaleTimer->start();
}
void MainUI::tabChanged(int tab){
if(tab==0){ ui->stackedWidget->setCurrentWidget(ui->page_current); }
else{ ui->stackedWidget->setCurrentWidget(ui->page_settings); }
ui->frame_modify->setVisible(tab==0);
}
void MainUI::showPopupsChanged(bool show){
settings->setValue("showPopupWarnings", show);
}
bool MainUI::getWindow(){
//Use this function to set cwin
cwin = 0;
snapArea = QRect(); //clear this too
//Save all the current settings for later
settings->setValue("screenshot-delay", ui->spin_delay->value());
if(ui->radio_window->isChecked()){
settings->setValue("screenshot-target", "window");
this->grabMouse( QCursor(Qt::CrossCursor) );
mousegrabbed = true;
this->centralWidget()->setEnabled(false);
this->setWindowOpacity(0);
return false; //wait for the next click to continue
}else if(ui->radio_area->isChecked()){
settings->setValue("screenshot-target", "area");
this->grabMouse( QCursor(Qt::CrossCursor) );
mousegrabbed = true;
this->centralWidget()->setEnabled(false);
this->setWindowOpacity(0);
return false; //wait for the next click to continue
}else if(ui->radio_monitor->isChecked()){
//will auto-grab the proper monitor later
}else{
settings->setValue("screenshot-target", "desktop");
}
return true;
}
void MainUI::getPixmap(){
QScreen *scrn = QApplication::screens().at(0);
QPixmap cpic;
//qDebug() << "Grab Pixmap:" << cwin;
if( (cwin==0 && ui->radio_window->isChecked() ) || ui->radio_all->isChecked() ){
//Grab the whole screen
cpic = scrn->grabWindow(QApplication::desktop()->winId());
}else if(cwin==0 && ui->radio_monitor->isChecked()){
QRect geom = QGuiApplication::screens().at(ui->spin_monitor->value()-1)->availableGeometry();
cpic = scrn->grabWindow(QApplication::desktop()->winId(), geom.x(), geom.y(), geom.width(), geom.height() );
}else if(cwin==0 && ui->radio_area->isChecked()){
//Grab the section of the screen which was selected
//qDebug() << "Screen Area:" << snapArea;
cpic = scrn->grabWindow(QApplication::desktop()->winId(), snapArea.x(), snapArea.y(), snapArea.width(), snapArea.height() );
}else{
//Grab just the designated window
if(ui->check_frame->isChecked()){
QRect geom = XCB->WindowGeometry(cwin, true); //include the frame
cpic = scrn->grabWindow(QApplication::desktop()->winId(), geom.x(), geom.y(), geom.width(), geom.height() );
}else{
cpic = scrn->grabWindow(cwin);
}
}
this->showNormal();
this->setGeometry(lastgeom);
lastScreenShot = QDateTime::currentDateTime();
//Now display the pixmap on the label as well
picSaved = false;
IMG->LoadImage( cpic.toImage() );
tabbar->setCurrentIndex(0);
}
void MainUI::mousePressEvent(QMouseEvent *ev){
if(mousegrabbed && ui->radio_area->isChecked()){
pt_click = ev->globalPos();
if(areaOverlay == 0){
areaOverlay = new QWidget(0, Qt::Window | Qt::BypassWindowManagerHint | Qt::WindowStaysOnTopHint);
areaOverlay->setWindowOpacity(0.5);
areaOverlay->setStyleSheet("background-color: rgba(150,150,150,120)");
}
}
QMainWindow::mouseMoveEvent(ev);
}
void MainUI::mouseMoveEvent(QMouseEvent *ev){
if(mousegrabbed && ui->radio_area->isChecked()){
//Not used yet - do something to paint the area so the user can see which area is selected
QRect area = pointsToRect(pt_click, ev->globalPos());
areaOverlay->setGeometry(area);
areaOverlay->show();
}else{
QMainWindow::mouseMoveEvent(ev);
}
}
void MainUI::mouseReleaseEvent(QMouseEvent *ev){
if(mousegrabbed){
mousegrabbed = false;
this->centralWidget()->setEnabled(true);
this->releaseMouse();
this->setWindowOpacity(1);
if(ui->radio_area->isChecked()){
//Need to determind the rectange which covers the area selected
areaOverlay->hide();
snapArea = pointsToRect(pt_click, ev->globalPos());
}else{
//In the middle of selecting a window to take a screenshot
// Get the window underneath the mouse click and take the screenshot
QList<WId> wins = XCB->WindowList();
QList<WId> stack = XCB->WM_Get_Client_List(true);
cwin = 0;
//qDebug() << "Try to select window:" << ev->globalPos() << ev->pos() << QCursor::pos();
for(int i=stack.length()-1; i>=0 && cwin==0; i--){ //work top->bottom in the stacking order
if(!wins.contains(stack[i])){ continue; }
if( XCB->WindowGeometry(stack[i], true).contains(ev->globalPos()) && XCB->WindowState(stack[i])!=LXCB::INVISIBLE ){
//qDebug() << "Found Window:" << i << XCB->WindowClass(stack[i]) << XCB->WindowGeometry(stack[i], true);
cwin = stack[i];
}
}
//qDebug() << " - Got window:" << cwin;
if(cwin==this->winId()){ return; } //cancelled
}
this->hide();
QTimer::singleShot(300+ui->spin_delay->value()*1000, this, SLOT(getPixmap()));
}else{
QMainWindow::mouseReleaseEvent(ev); //normal processing
}
}
void MainUI::resizeEvent(QResizeEvent*){
IMG->setDefaultSize( ui->scrollArea->maximumViewportSize() );
}
void MainUI::closeEvent(QCloseEvent *ev){
//qDebug() << "Close Event:" << ui->check_show_popups->isChecked() << picSaved;
if(ui->check_show_popups->isChecked() && !picSaved){
//Ask what to do about the unsaved changed
QMessageBox dialog( QMessageBox::Warning, tr("Unsaved Screenshot"),
tr("The current screenshot has not been saved yet. Do you want to save or discard your changes?"),
QMessageBox::Discard | QMessageBox::Save | QMessageBox::Cancel, this);
dialog.setDefaultButton(QMessageBox::Cancel);
dialog.setButtonText(QMessageBox::Save, tr("Save"));
dialog.setButtonText(QMessageBox::Discard, tr("Discard"));
dialog.setButtonText(QMessageBox::Cancel, tr("Cancel"));
switch (dialog.exec()) {
case QMessageBox::Discard:
// Just close, we don't care about the file.
break;
case QMessageBox::Save:
closeOnSave = true;
saveScreenshot();
// fall through
case QMessageBox::Cancel:
ev->ignore();
return;
}
}
QMainWindow::closeEvent(ev);
}
void MainUI::quitShortcut_activated(){
QApplication::quit();
}
|