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 <QLineEdit>
18#include <QListWidget>
19#include <QLabel>
20#include <QTableWidget>
21#include <QVBoxLayout>
22#include <QGridLayout>
23#include <QHeaderView>
24
25#include <kdebug.h>
26
27#include "imagemapchoosedialog.h"
28
29ImageMapChooseDialog::ImageMapChooseDialog(
30 QWidget* parent,
31 QList<MapTag*> _maps,
32 QList<ImageTag*> _images,
33 const KUrl & _baseUrl)
34 : KDialog(parent)
35{
36 kDebug() << "ImageMapChooseDialog::ImageMapChooseDialog";
37 kWarning(parent == 0) << "ImageMapChooseDialog: parent is null!";
38
39 setCaption(i18n( "Choose Map & Image to Edit" ));
40 setModal(true);
41 setButtons(Ok);
42 setDefaultButton(Ok);
43 showButtonSeparator(true);
44 baseUrl=_baseUrl;
45 maps=_maps;
46 images=_images;
47// currentMap;
48 QWidget *page=new QWidget(this);
49 setMainWidget(page);
50 setCaption(baseUrl.fileName());
51 QVBoxLayout *layout = new QVBoxLayout(page);//,5,5);
52
53 QLabel *lbl= new QLabel(i18n("Select an image and/or a map that you want to edit"),page);
54 lbl->setFont(QFont("Sans Serif",12, QFont::Bold));
55 layout->addWidget(lbl);
56 QFrame *line= new QFrame(page);
57 line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
58 line->setFixedHeight(10);
59 layout->addWidget(line,0);
60
61 QGridLayout *gridLayout= new QGridLayout();
62 layout->addLayout(gridLayout);
63 gridLayout->setRowStretch(0,0);
64 gridLayout->setRowStretch(1,100);
65 lbl=new QLabel(i18n("&Maps"),page);
66 mapListBox= new QListWidget(page);
67 lbl->setBuddy(mapListBox);
68 gridLayout->addWidget(lbl,0,0);
69 gridLayout->addWidget(mapListBox,1,0);
70
71 line= new QFrame(page);
72 line->setFrameStyle(QFrame::VLine | QFrame::Sunken);
73 line->setFixedWidth(10);
74// line->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
75 gridLayout->addWidget(line,1,1);
76
77 lbl=new QLabel(i18n("Image Preview"),page);
78 gridLayout->addWidget(lbl,0,2);
79
80 imagePreview= new QLabel(page);
81 imagePreview->setFixedSize(310,210);
82 imagePreview->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
83 imagePreview->setFrameStyle(QLabel::Panel | QLabel::Sunken);
84 imagePreview->setIndent(5);
85 //imagePreview->setBackground(QBrush(QColor("white")));
86// imagePreview->setLineWidth(2);
87// imagePreview->setScaledContents(true);
88// lbl= new QLabel(i18n("&Maps"),page);
89// lbl->setBuddy(mapListBox);
90 gridLayout->addWidget(imagePreview,1,2);
91// layout->addLayout(gridLayout,1);
92
93 line= new QFrame(page);
94 line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
95 line->setFixedHeight(10);
96 layout->addWidget(line,0);
97
98
99 if (maps.isEmpty()) {
100 mapListBox->addItem(i18n("No maps found"));
101 mapListBox->setEnabled(false);
102 } else {
103 for (int i = 0; i < maps.count(); i++) {
104 mapListBox->addItem(maps.at(i)->name);
105 }
106 kDebug() << "ImageMapChooseDialog::ImageMapChooseDialog: before connect ";
107 // UNSOLVED CRASH: connect(mapListBox, SIGNAL(currentRowChanged(int)), this, SLOT(slotMapChanged(int)));
108 }
109
110 kDebug() << "ImageMapChooseDialog::ImageMapChooseDialog: before call initImageListTable ";
111 initImageListTable(page);
112
113 if (! maps.isEmpty()) {
114 mapListBox->setCurrentItem(0);
115 slotMapChanged(0);
116 }
117
118 resize(510,460);
119}
120
121void ImageMapChooseDialog::initImageListTable(QWidget* parent) {
122 kDebug() << "ImageMapChooseDialog::initImageListTable ";
123
124
125 if (images.isEmpty()) {
126 imageListTable= new QTableWidget(1,1,parent);
127 imageListTable->setItem(0,0, new QTableWidgetItem(i18n("No images found")));
128 imageListTable->setEnabled(false);
129 imageListTable->horizontalHeader()->hide();
130 // PORT: imageListTable->setTopMargin(0);
131 // PORT: imageListTable->setColumnStretchable(0,true);
132 } else {
133 imageListTable= new QTableWidget(images.count(),2,parent);
134 // PORT: imageListTable->setColumnStretchable(0,true);
135 }
136
137 imageListTable->verticalHeader()->hide();
138 // PORT imageListTable->setLeftMargin(0);
139
140 QLabel *lbl= new QLabel(i18n("&Images"),parent);
141 lbl->setBuddy(imageListTable);
142
143 parent->layout()->addWidget(lbl);
144 parent->layout()->addWidget(imageListTable);
145
146 if (images.isEmpty())
147 return;
148
149 imageListTable->setHorizontalHeaderLabels(QStringList() << i18n("Path") << "usemap");
150
151 imageListTable->setSelectionMode(QAbstractItemView::SingleSelection);
152 // PORT: imageListTable->setFocusStyle(QTableWidget::FollowStyle);
153 imageListTable->clearSelection();
154
155
156 int row=0;
157 QListIterator<ImageTag*> it(images);
158 while (it.hasNext()) {
159 QString src="";
160 QString usemap="";
161 ImageTag* tag = it.next();
162 if (tag->contains("src"))
163 src=tag->value("src");
164 if (tag->contains("usemap"))
165 usemap=tag->value("usemap");
166
167 imageListTable->setItem(row,0, new QTableWidgetItem(src));
168 imageListTable->setItem(row,1, new QTableWidgetItem(usemap));
169 row++;
170 }
171
172 // UNSOLVED CRASH: connect (imageListTable, SIGNAL(itemSelectionChanged()),
173 // this, SLOT(slotImageChanged()));
174
175 imageListTable->selectRow(0);
176 slotImageChanged();
177
178
179}
180
181ImageMapChooseDialog::~ImageMapChooseDialog() {
182}
183
184void ImageMapChooseDialog::slotImageChanged()
185{
186 kDebug() << "ImageMapChooseDialog::slotImageChanged";
187 int i=imageListTable->currentRow();
188 if (i < 0 || i > images.count())
189 i = 0;
190 QImage pix;
191 if (images.at(i)->contains("src")) {
192 QString str = images.at(i)->value("src");
193 // relative url
194 pixUrl=KUrl(baseUrl,str);
195 pix=QImage(pixUrl.path());
196 double zoom1=1;
197 double zoom2=1;
198 if (pix.width()>300)
199 zoom1=(double) 300/pix.width();
200 if (pix.height()>200)
201 zoom2=(double) 200/pix.height();
202
203
204 zoom1= zoom1 < zoom2 ? zoom1 : zoom2;
205 pix=pix.scaled((int)(pix.width()*zoom1),
206 int(pix.height()*zoom1),
207 Qt::KeepAspectRatio,
208 Qt::SmoothTransformation);
209 }
210 QPixmap pix2;
211 pix2.fromImage(pix);
212 imagePreview->setPixmap(pix2);
213
214// imagePreview->repaint();
215}
216
217
218void ImageMapChooseDialog::selectImageWithUsemap(const QString & usemap) {
219 kDebug() << "ImageMapChooseDialog::selectImageWithUsemap: " << usemap;
220
221 for (int i=0; i<imageListTable->rowCount(); i++) {
222 QTableWidgetItem *item = imageListTable->item(i,1);
223 if (item && (item->text()==usemap)) {
224 imageListTable->selectRow(i);
225 slotImageChanged();
226 return;
227 }
228 }
229}
230
231void ImageMapChooseDialog::slotMapChanged(int i) {
232 kDebug() << "ImageMapChooseDialog::slotMapChanged: " << i;
233 currentMap=maps.at(i);
234 selectImageWithUsemap(currentMap->name);
235}
236
237#include "imagemapchoosedialog.moc"
238