1 /***************************************************************************
2 imagemapchoosedialog.cpp - description
3 -------------------
4 begin : 06-03-2007
5 copyright : (C) 2007 by Jan Schäfer
6 email : j_schaef@informatik.uni-kl.de
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17 #include "imagemapchoosedialog.h"
18
19 #include <QDialogButtonBox>
20 #include <QDir>
21 #include <QGridLayout>
22 #include <QHeaderView>
23 #include <QLabel>
24 #include <QLineEdit>
25 #include <QListWidget>
26 #include <QPushButton>
27 #include <QTableWidget>
28 #include <QVBoxLayout>
29
30 #include <KConfigGroup>
31
32 #include "kimagemapeditor_debug.h"
33
ImageMapChooseDialog(QWidget * parent,QList<MapTag * > _maps,QList<ImageTag * > _images,const QUrl & _baseUrl)34 ImageMapChooseDialog::ImageMapChooseDialog(
35 QWidget* parent,
36 QList<MapTag*> _maps,
37 QList<ImageTag*> _images,
38 const QUrl & _baseUrl)
39 : QDialog(parent)
40 {
41 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog";
42 if (parent == nullptr) {
43 qCWarning(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog: parent is null!";
44 }
45
46 setWindowTitle(i18n( "Choose Map & Image to Edit" ));
47 setModal(true);
48 baseUrl = _baseUrl;
49 maps = _maps;
50 images = _images;
51 // currentMap;
52 setWindowTitle(baseUrl.fileName());
53 QVBoxLayout *layout = new QVBoxLayout(this);
54
55 QLabel *lbl = new QLabel(i18n("Select an image and/or a map that you want to edit"));
56 lbl->setFont(QFont("Sans Serif",12, QFont::Bold));
57 layout->addWidget(lbl);
58 QFrame *line = new QFrame;
59 line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
60 line->setFixedHeight(10);
61 layout->addWidget(line,0);
62
63 QGridLayout *gridLayout = new QGridLayout;
64 layout->addLayout(gridLayout);
65 gridLayout->setRowStretch(0, 0);
66 gridLayout->setRowStretch(1, 100);
67 lbl = new QLabel(i18n("&Maps"));
68 mapListBox = new QListWidget;
69 lbl->setBuddy(mapListBox);
70 gridLayout->addWidget(lbl, 0, 0);
71 gridLayout->addWidget(mapListBox, 1, 0);
72
73 line = new QFrame;
74 line->setFrameStyle(QFrame::VLine | QFrame::Sunken);
75 line->setFixedWidth(10);
76 // line->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
77 gridLayout->addWidget(line, 1, 1);
78
79 lbl = new QLabel(i18n("Image Preview"));
80 gridLayout->addWidget(lbl, 0, 2);
81
82 imagePreview = new QLabel;
83 imagePreview->setFixedSize(310,210);
84 imagePreview->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
85 imagePreview->setFrameStyle(QLabel::Panel | QLabel::Sunken);
86 imagePreview->setIndent(5);
87 //imagePreview->setBackground(QBrush(QColor("white")));
88 // imagePreview->setLineWidth(2);
89 // imagePreview->setScaledContents(true);
90 // lbl = new QLabel(i18n("&Maps"),page);
91 // layout->addWidget(lbl);
92 // lbl->setBuddy(mapListBox);
93 gridLayout->addWidget(imagePreview, 1, 2);
94 // layout->addLayout(gridLayout,1);
95
96 line = new QFrame;
97 layout->addWidget(line);
98 line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
99 line->setFixedHeight(10);
100 layout->addWidget(line, 0);
101
102
103 if (maps.isEmpty()) {
104 mapListBox->addItem(i18n("No maps found"));
105 mapListBox->setEnabled(false);
106 } else {
107 for (int i = 0; i < maps.count(); i++) {
108 mapListBox->addItem(maps.at(i)->name);
109 }
110 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog: before connect ";
111 // UNSOLVED CRASH: connect(mapListBox, SIGNAL(currentRowChanged(int)), this, SLOT(slotMapChanged(int)));
112 }
113
114 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog: before call initImageListTable ";
115 initImageListTable(layout);
116
117 if (! maps.isEmpty()) {
118 mapListBox->setCurrentItem(nullptr);
119 slotMapChanged(0);
120 }
121
122 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
123 QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
124 okButton->setDefault(true);
125 okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
126 connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
127 connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
128 okButton->setDefault(true);
129 layout->addWidget(buttonBox);
130
131 resize(510,460);
132 }
133
initImageListTable(QLayout * layout)134 void ImageMapChooseDialog::initImageListTable(QLayout* layout) {
135 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::initImageListTable ";
136
137
138 if (images.isEmpty()) {
139 imageListTable = new QTableWidget(1, 1);
140 imageListTable->setItem(0,0, new QTableWidgetItem(i18n("No images found")));
141 imageListTable->setEnabled(false);
142 imageListTable->horizontalHeader()->hide();
143 // PORT: imageListTable->setTopMargin(0);
144 // PORT: imageListTable->setColumnStretchable(0,true);
145 } else {
146 imageListTable = new QTableWidget(images.count(), 2);
147 // PORT: imageListTable->setColumnStretchable(0,true);
148 }
149
150 imageListTable->verticalHeader()->hide();
151 // PORT imageListTable->setLeftMargin(0);
152
153 QLabel *lbl = new QLabel(i18n("&Images"));
154 lbl->setBuddy(imageListTable);
155
156 layout->addWidget(lbl);
157 layout->addWidget(imageListTable);
158
159 if (images.isEmpty())
160 return;
161
162 imageListTable->setHorizontalHeaderLabels(QStringList() << i18n("Path") << "usemap");
163
164 imageListTable->setSelectionMode(QAbstractItemView::SingleSelection);
165 // PORT: imageListTable->setFocusStyle(QTableWidget::FollowStyle);
166 imageListTable->clearSelection();
167
168
169 int row=0;
170 QListIterator<ImageTag*> it(images);
171 while (it.hasNext()) {
172 QString src="";
173 QString usemap="";
174 ImageTag* tag = it.next();
175 if (tag->contains("src"))
176 src=tag->value("src");
177 if (tag->contains("usemap"))
178 usemap=tag->value("usemap");
179
180 imageListTable->setItem(row,0, new QTableWidgetItem(src));
181 imageListTable->setItem(row,1, new QTableWidgetItem(usemap));
182 row++;
183 }
184
185 // UNSOLVED CRASH: connect (imageListTable, SIGNAL(itemSelectionChanged()),
186 // this, SLOT(slotImageChanged()));
187
188 imageListTable->selectRow(0);
189 slotImageChanged();
190
191
192 }
193
~ImageMapChooseDialog()194 ImageMapChooseDialog::~ImageMapChooseDialog() {
195 }
196
slotImageChanged()197 void ImageMapChooseDialog::slotImageChanged()
198 {
199 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::slotImageChanged";
200 int i=imageListTable->currentRow();
201 if (i < 0 || i > images.count())
202 i = 0;
203 QImage pix;
204 if (images.at(i)->contains("src")) {
205 QString str = images.at(i)->value("src");
206 // relative url
207 if (baseUrl.path().isEmpty() | !baseUrl.path().endsWith('/')) {
208 pixUrl=QUrl(baseUrl.path() + '/').resolved(QUrl(str));
209 }
210 else {
211 pixUrl=baseUrl.resolved(QUrl(str));
212 }
213 pix=QImage(pixUrl.path());
214 double zoom1=1;
215 double zoom2=1;
216 if (pix.width()>300)
217 zoom1=(double) 300/pix.width();
218 if (pix.height()>200)
219 zoom2=(double) 200/pix.height();
220
221
222 zoom1= zoom1 < zoom2 ? zoom1 : zoom2;
223 pix=pix.scaled((int)(pix.width()*zoom1),
224 int(pix.height()*zoom1),
225 Qt::KeepAspectRatio,
226 Qt::SmoothTransformation);
227 }
228 QPixmap pix2;
229 pix2.fromImage(pix);
230 imagePreview->setPixmap(pix2);
231
232 // imagePreview->repaint();
233 }
234
235
selectImageWithUsemap(const QString & usemap)236 void ImageMapChooseDialog::selectImageWithUsemap(const QString & usemap) {
237 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::selectImageWithUsemap: " << usemap;
238
239 for (int i=0; i<imageListTable->rowCount(); i++) {
240 QTableWidgetItem *item = imageListTable->item(i,1);
241 if (item && (item->text()==usemap)) {
242 imageListTable->selectRow(i);
243 slotImageChanged();
244 return;
245 }
246 }
247 }
248
slotMapChanged(int i)249 void ImageMapChooseDialog::slotMapChanged(int i) {
250 qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::slotMapChanged: " << i;
251 currentMap=maps.at(i);
252 selectImageWithUsemap(currentMap->name);
253 }
254
255