forked from gnudles/stereograma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagecontainerwidget.cpp
166 lines (148 loc) · 4.06 KB
/
imagecontainerwidget.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
#include "imagecontainerwidget.h"
#include <QDragEnterEvent>
#include <QUrl>
#include <QSettings>
#include "imagefiledialog.h"
#include <QMessageBox>
#include <QMimeData>
ImageContainerWidget::ImageContainerWidget(QWidget *parent) :
QLabel(parent)
{
connect(&netManager, SIGNAL(finished(QNetworkReply*)),
SLOT(downloadFinished(QNetworkReply*)));
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)),
SLOT(showContextMenu(const QPoint&)));
_loadAction=myMenu.addAction("Load");
_saveAsAction=myMenu.addAction("Save As...");
_viewAction=myMenu.addAction("View");
_unloadAction=myMenu.addAction("Unload");
m_viewer=new ImageViewer(this);
setAcceptDrops(true);
}
const QString & ImageContainerWidget::getText()
{
if (replaceText.isNull())
{
replaceText=text();
}
return replaceText;
}
void ImageContainerWidget::mySetPixmap()
{
if (replaceText.isNull())
{
replaceText=text();
}
if (imdata.isNull())
{
setAlignment(Qt::AlignLeft | Qt::AlignTop);
setText(replaceText);
}
else
{
setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
setScaledContents(false);
QSize size = QSize(width(),height());
QImage smallimage=imdata.convertToFormat(QImage::Format_ARGB32).scaled(size, Qt::KeepAspectRatio);
setPixmap(QPixmap::fromImage(smallimage));
}
}
void ImageContainerWidget::setImage(const QString &fileName)
{
if(!imdata.load(fileName))
QMessageBox::warning(this,"Error","Unrecognized image format");
mySetPixmap();
}
void ImageContainerWidget::setImage(const QImage &image)
{
imdata=image;
mySetPixmap();
}
void ImageContainerWidget::downloadFinished(QNetworkReply* reply)
{
if (!reply->error())
{
if(!imdata.load((QIODevice*)reply,0))
QMessageBox::warning(this,"Error","Unrecognized image format");
mySetPixmap();
}
else
{
QMessageBox::warning(this,"Error","Could not download the file");
}
reply->deleteLater();
}
void ImageContainerWidget::showContextMenu(const QPoint& pos)
{
QPoint globalPos = mapToGlobal(pos);
QAction* selectedItem = myMenu.exec(globalPos);
if (selectedItem == _saveAsAction)
{
saveDialog();
}
else if (selectedItem == _unloadAction)
{
imdata= QImage();
mySetPixmap();
}
else if(selectedItem== _loadAction)
{
openDialog();
}
else if(selectedItem== _viewAction)
{
m_viewer->setFolderSettings(folderSettings);
m_viewer->setSaveTitle(saveTitle);
m_viewer->setImage(imdata);
m_viewer->show();
}
}
void ImageContainerWidget::saveDialog()
{
saveAsImage();
}
void ImageContainerWidget::openDialog()
{
QSettings settings;
QString cdir=settings.value(folderSettings,folderSettings).toString();
ImageFileDialog dlg(this);
dlg.init();
dlg.setFileMode( QFileDialog::ExistingFile);
dlg.setAcceptMode(QFileDialog::AcceptOpen);
dlg.setDirectory(cdir);
dlg.setNameFilter(tr("Images (*.png *.xpm *.jpg *.jpeg *.bmp *.gif *.tiff *.tif *.PNG *.XPM *.JPG *.JPEG *.BMP *.GIF *.TIF *.TIFF)"));
dlg.setWindowTitle("Open "+getText());
QString fileName;
if (dlg.exec())
{
fileName = dlg.selectedFiles().first();
settings.setValue(folderSettings, QFileInfo(fileName).absoluteDir().absolutePath());
setImage(fileName);
}
dlg.deinit();
}
void ImageContainerWidget::mouseDoubleClickEvent(QMouseEvent * )
{
openDialog();
}
void ImageContainerWidget::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/uri-list"))
event->acceptProposedAction();
}
void ImageContainerWidget::dropEvent(QDropEvent *event)
{
QList<QUrl> urls = event->mimeData()->urls();
if (urls.isEmpty())
return;
QString fileName = urls.first().toLocalFile();
if (fileName.isEmpty())
{
QNetworkRequest request(urls.first());
netManager.get(request);
}
else
{
setImage(fileName);
}
}