summaryrefslogtreecommitdiffstats
path: root/pictureviewer2.cpp
blob: 0c2530edd7d1dd8259b082f4af9c4e0856e4a1e1 (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
/*
  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.
*/

#include <QDesktopWidget>
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsPixmapItem>
#include <QImage>

#include "pictureviewer2.h"

const QString PictureViewer2::mDefaultFile(":/picgone.png");

PictureViewer2::PictureViewer2(QWidget *parent) : QGraphicsView(parent), mCur(0) {
    //behave like QDialog, but don't be one...
    setWindowFlags(QFlags<Qt::WindowType>(0x1|0x2|0x1000|0x2000|0x10000|0x8000000));

    //setup gui
    mScene = new QGraphicsScene(this);
    setScene(mScene);
    QDesktopWidget *dw = QApplication::desktop();
    QSize screenSize = dw->availableGeometry().size();
    mMinimunSize = QSize(screenSize.width() - 100, screenSize.height() - 100);
    setMinimumSize(mMinimunSize);
    move(50, 50);
    setVisible(false);
    setFile();
}

void PictureViewer2::setFile(const QString file){
    QPixmap pixmap(file);
    if(pixmap.isNull()){
        return;
    }
    setCurPalette(pixmap);
    if((pixmap.height() > height()) || (pixmap.width() > width())){
        pixmap = pixmap.scaled(QSize(width() - 3, height() - 10), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    }
    if(mCur){
        mScene->removeItem(mCur);
        mCur = 0;
    }
    mCur = new QGraphicsPixmapItem(pixmap);
    mScene->addItem(mCur);
}

void PictureViewer2::setCurPalette(const QPixmap &pic){
    QPoint val1 = QPoint(qrand() % pic.width(), qrand() % pic.height());
    QPoint val2 = QPoint(qrand() % pic.width(), qrand() % pic.height());
    QImage img = pic.toImage();
    QColor c1(img.pixel(val1));
    QColor c2(img.pixel(val2));
    QLinearGradient g(QPoint(0,0), QPoint(rect().bottomRight()));
    g.setColorAt(0, c1);
    g.setColorAt(1, c2);
    setBackgroundBrush(QBrush(g));
}

QSize PictureViewer2::sizeHint() const{
    return mMinimunSize;
}