summaryrefslogtreecommitdiffstats
path: root/examples/Screenshot.qs
blob: ead2a32b2dfdd105a02e8b5948f2a2bd59a9cd74 (plain)
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
/****************************************************************************
**
** Copyright (C) 2008 Trolltech ASA. All rights reserved.
**
** This file is part of the Qt Script Generator project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

function tr(s) { return s; }

function Screenshot(parent) { 
    QWidget.call(this, parent);

    this.screenshotLabel = new QLabel();
    this.screenshotLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding);
    this.screenshotLabel.alignment = Qt.Alignment(Qt.AlignCenter);
    this.screenshotLabel.setMinimumSize(240, 160);
    
    this.createOptionsGroupBox();
    this.createButtonsLayout();
    
    this.mainLayout = new QVBoxLayout();
    this.mainLayout.addWidget(this.screenshotLabel,0 ,0);
    this.mainLayout.addWidget(this.optionsGroupBox,0 ,0);
    this.mainLayout.addLayout(this.buttonsLayout);
    this.setLayout(this.mainLayout);
    
    this.shootScreen();
    this.delaySpinBox.setValue(5);
    
    this.windowIcon = new QIcon("classpath:com/trolltech/images/qt-logo.png");
    this.windowTitle = tr("Screenshot");
    this.resize(300, 200);
}

Screenshot.prototype = new QWidget();

Screenshot.prototype.resizeEvent = function(event) {
    var scaledSize = this.originalPixmap.size();
    scaledSize.scale(this.screenshotLabel.size, Qt.AspectRatioMode.KeepAspectRatio);
    if (this.screenshotLabel.pixmap != null 
        || scaledSize != this.screenshotLabel.pixmap.size())
        this.updateScreenshotLabel();
}

Screenshot.prototype.newScreenshot = function() {
    if ( this.hideThisWindowCheckBox.checked)
        this.hide();
    this.newScreenshotButton.setDisabled(true);

    //FIXME    
    // QTimer.singleShot(this.delaySpinBox.value * 1000, 
    //                      this, this.shootScreen);  
    var singleShot = new QTimer();
    singleShot.singleShot = true;
    singleShot.timeout.connect(this, this.shootScreen);
    singleShot.start(this.delaySpinBox.value * 1000);
}

Screenshot.prototype.saveScreenshot = function() {
    var format = "png";
    var initialPath = QDir.currentPath() + tr("/untitled.") + format;
    var filter = tr(format.toUpperCase() + " Files (*." + format + ");;All Files (*)");
    var fileName = QFileDialog.getSaveFileName(this, tr("Save As"), initialPath, filter, null, null);
    // new QFileDialog.Option.Filter(filter)); //FIXME
    
    if (fileName != "")
        this.originalPixmap.save(fileName); //, format); //FIXME
}

Screenshot.prototype.shootScreen = function() {
    if ( this.delaySpinBox.value != 0)
        QApplication.beep();
    
    this.originalPixmap = null;
    
    this.originalPixmap = QPixmap.grabWindow(
        QApplication.desktop().winId());
    this.updateScreenshotLabel();
    
    this.newScreenshotButton.enabled = true;
    if (this.hideThisWindowCheckBox.checked)
        this.show();
}

Screenshot.prototype.updateCheckBox = function() {
    if (this.delaySpinBox.value)
        this.hideThisWindowCheckBox.setDisabled(true);
    else
        this.hideThisWindowCheckBox.setDisabled(false);
}

Screenshot.prototype.createOptionsGroupBox = function() {
    this.optionsGroupBox = new QGroupBox(tr("Options"));
    
    this.delaySpinBox = new QSpinBox();
    this.delaySpinBox.suffix = tr(" s");
    this.delaySpinBox.maximum = 60;
    this.delaySpinBox.valueChanged.connect(this, this.updateCheckBox);
    
    this.delaySpinBoxLabel = new QLabel(tr("Screenshot Delay:"));
    
    this.hideThisWindowCheckBox = new QCheckBox(tr("Hide This Window"));
    
    this.optionsGroupBoxLayout = new QGridLayout();
    this.optionsGroupBoxLayout.addWidget(this.delaySpinBoxLabel, 0, 0);
    this.optionsGroupBoxLayout.addWidget(this.delaySpinBox, 0, 1);
    this.optionsGroupBoxLayout.addWidget(this.hideThisWindowCheckBox, 1, 0, 1, 2);
    this.optionsGroupBox.setLayout(this.optionsGroupBoxLayout);
}

Screenshot.prototype.createButtonsLayout = function() {
    this.newScreenshotButton = this.createButton(tr("New Screenshot"), this, 
                                            this.newScreenshot);
    
    this.saveScreenshotButton = this.createButton(tr("Save Screenshot"), this, 
                                            this.saveScreenshot);

    this.quitScreenshotButton = this.createButton(tr("Quit"), this, this.close);

    this.buttonsLayout = new QHBoxLayout();
    this.buttonsLayout.addStretch();
    this.buttonsLayout.addWidget(this.newScreenshotButton, 0, 0);
    this.buttonsLayout.addWidget(this.saveScreenshotButton, 0, 0);
    this.buttonsLayout.addWidget(this.quitScreenshotButton, 0, 0);
}

Screenshot.prototype.createButton = function(text, receiver, member) {
    var button = new QPushButton(text);
    button.clicked.connect(receiver, member);
    return button;
}

Screenshot.prototype.updateScreenshotLabel = function() {
    this.screenshotLabel.setPixmap(this.originalPixmap.scaled(this.screenshotLabel.size,
                                                              Qt.KeepAspectRatio, 
                                                              Qt.SmoothTransformation));
}

var screenshot = new Screenshot(null);

screenshot.show();
QCoreApplication.exec();