on xenial help sign in
Rotate the screenshots according to the actual orientation
commitf79facfa005586890714252978a437561fd6bab5 authorLukáš Tinkl <lukas.tinkl@canonical.com> dateWed 21 Oct 2015, 11:50 (11 years ago) parent385d92ae587bd6
6 files changed+20−6
plugins/ScreenGrabber/screengrabber.cpp+2−2 plugins/ScreenGrabber/screengrabber.h+1−1 qml/Components/ScreenGrabber.qml+4−1 qml/Shell.qml+1−1 tests/plugins/ScreenGrabber/ScreenGrabberTest.cpp+11 tests/plugins/ScreenGrabber/grabber.qml+1−1
plugins/ScreenGrabber/screengrabber.cpp+2−2
@@ -60,7 +60,7 @@ ScreenGrabber::ScreenGrabber(QObject *parent)
6060 }
6161}
6262
63−void ScreenGrabber::captureAndSave()
63+void ScreenGrabber::captureAndSave(int angle)
6464{
6565 if (fileNamePrefix.isEmpty())
6666 {
@@ -82,7 +82,7 @@ void ScreenGrabber::captureAndSave()
8282 return;
8383 }
8484
85− const QImage screenshot = main_window->grabWindow();
85+ const QImage screenshot = main_window->grabWindow().transformed(QTransform().rotate(angle));
8686 const QString filename = makeFileName();
8787 qDebug() << "Saving screenshot to" << filename;
8888 auto saveOp = QtConcurrent::run(saveScreenshot, screenshot, filename, getFormat(), screenshotQuality);
plugins/ScreenGrabber/screengrabber.h+1−1
@@ -29,7 +29,7 @@ public:
2929 ~ScreenGrabber() = default;
3030
3131public Q_SLOTS:
32− void captureAndSave();
32+ void captureAndSave(int angle = 0);
3333
3434Q_SIGNALS:
3535 void screenshotSaved(const QString &filename);
qml/Components/ScreenGrabber.qml+4−1
@@ -26,6 +26,9 @@ Rectangle {
2626 anchors.fill: parent
2727 opacity: 0.0
2828
29+ // to be set from outside
30+ property int rotationAngle: 0
31+
2932 ScreenGrabber {
3033 id: screenGrabber
3134 objectName: "screenGrabber"
@@ -67,7 +70,7 @@ Rectangle {
6770 to: 0.0
6871 onStopped: {
6972 if (visible) {
70− screenGrabber.captureAndSave();
73+ screenGrabber.captureAndSave(root.rotationAngle);
7174 visible = false;
7275 }
7376 }
qml/Shell.qml+1−1
@@ -659,6 +659,7 @@ Item {
659659
660660 ScreenGrabber {
661661 id: screenGrabber
662+ rotationAngle: -shell.orientationAngle
662663 z: dialogs.z + 10
663664 }
664665
@@ -687,5 +688,4 @@ Item {
687688 }
688689 }
689690 }
690−
691691}
tests/plugins/ScreenGrabber/ScreenGrabberTest.cpp+11
@@ -55,6 +55,17 @@ private Q_SLOTS:
5555 QVERIFY(!args.first().toString().isEmpty());
5656 }
5757
58+ void testRotatedScreenshot()
59+ {
60+ QSignalSpy grabberSpy(m_grabber, &ScreenGrabber::screenshotSaved);
61+ m_grabber->captureAndSave(90); // rotate by 90°
62+ const QVariantList args = grabberSpy.takeFirst();
63+ const QString filename = args.first().toString();
64+ QVERIFY(!filename.isEmpty());
65+ QImage img(filename);
66+ QVERIFY(img.height() > img.width()); // verify that the image got rotated by 90° (height > width)
67+ }
68+
5869private:
5970 QQuickView *m_view;
6071 ScreenGrabber *m_grabber = nullptr;
tests/plugins/ScreenGrabber/grabber.qml+1−1
@@ -20,7 +20,7 @@ import ScreenGrabber 0.1
2020Rectangle {
2121 property var grabber: screenGrabber
2222
23− width: 100
23+ width: 101 // width intentionally bigger than height to test rotation
2424 height: 100
2525 color: "green"
2626