File indexing completed on 2024-04-21 04:20:14

0001 /***************************************************************************
0002                           imagemapchoosedialog.cpp  -  description
0003                             -------------------
0004     begin                : 06-03-2007
0005     copyright            : (C) 2007 by Jan Schäfer
0006     email                : j_schaef@informatik.uni-kl.de
0007 ***************************************************************************/
0008 
0009 /***************************************************************************
0010 *                                                                         *
0011 *   This program is free software; you can redistribute it and/or modify  *
0012 *   it under the terms of the GNU General Public License as published by  *
0013 *   the Free Software Foundation; either version 2 of the License, or     *
0014 *   (at your option) any later version.                                   *
0015 *                                                                         *
0016 ***************************************************************************/
0017 #include "imagemapchoosedialog.h"
0018 
0019 #include <QDialogButtonBox>
0020 #include <QDir>
0021 #include <QGridLayout>
0022 #include <QHeaderView>
0023 #include <QLabel>
0024 #include <QLineEdit>
0025 #include <QListWidget>
0026 #include <QPushButton>
0027 #include <QTableWidget>
0028 #include <QVBoxLayout>
0029 
0030 #include <KConfigGroup>
0031 
0032 #include "kimagemapeditor_debug.h"
0033 
0034 ImageMapChooseDialog::ImageMapChooseDialog(
0035     QWidget* parent,
0036     QList<MapTag*> _maps,
0037     QList<ImageTag*> _images,
0038     const QUrl & _baseUrl)
0039   : QDialog(parent)
0040 {
0041   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog";
0042   if (parent == nullptr) {
0043     qCWarning(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog: parent is null!";
0044   }
0045 
0046   setWindowTitle(i18n( "Choose Map & Image to Edit" ));
0047   setModal(true);
0048   baseUrl = _baseUrl;
0049   maps = _maps;
0050   images = _images;
0051 //  currentMap;
0052   setWindowTitle(baseUrl.fileName());
0053   QVBoxLayout *layout = new QVBoxLayout(this);
0054 
0055   QLabel *lbl = new QLabel(i18n("Select an image and/or a map that you want to edit"));
0056   lbl->setFont(QFont("Sans Serif",12, QFont::Bold));
0057   layout->addWidget(lbl);
0058   QFrame *line = new QFrame;
0059   line->setFrameStyle(QFrame::HLine | QFrame::Sunken);
0060   line->setFixedHeight(10);
0061   layout->addWidget(line,0);
0062 
0063   QGridLayout *gridLayout = new QGridLayout;
0064   layout->addLayout(gridLayout);
0065   gridLayout->setRowStretch(0, 0);
0066   gridLayout->setRowStretch(1, 100);
0067   lbl = new QLabel(i18n("&Maps"));
0068   mapListBox = new QListWidget;
0069   lbl->setBuddy(mapListBox);
0070   gridLayout->addWidget(lbl, 0, 0);
0071   gridLayout->addWidget(mapListBox, 1, 0);
0072 
0073   line = new QFrame;
0074   line->setFrameStyle(QFrame::VLine | QFrame::Sunken);
0075   line->setFixedWidth(10);
0076 //  line->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
0077   gridLayout->addWidget(line, 1, 1);
0078 
0079   lbl = new QLabel(i18n("Image Preview"));
0080   gridLayout->addWidget(lbl, 0, 2);
0081 
0082   imagePreview = new QLabel;
0083   imagePreview->setFixedSize(310,210);
0084   imagePreview->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
0085   imagePreview->setFrameStyle(QLabel::Panel | QLabel::Sunken);
0086   imagePreview->setIndent(5);
0087   //imagePreview->setBackground(QBrush(QColor("white")));
0088 //  imagePreview->setLineWidth(2);
0089 //  imagePreview->setScaledContents(true);
0090 //  lbl = new QLabel(i18n("&Maps"),page);
0091 //  layout->addWidget(lbl);
0092 //  lbl->setBuddy(mapListBox);
0093   gridLayout->addWidget(imagePreview, 1, 2);
0094 //  layout->addLayout(gridLayout,1);
0095 
0096   line = new QFrame;
0097   layout->addWidget(line);
0098   line->setFrameStyle(QFrame::HLine  | QFrame::Sunken);
0099   line->setFixedHeight(10);
0100   layout->addWidget(line, 0);
0101 
0102 
0103   if (maps.isEmpty()) {
0104     mapListBox->addItem(i18n("No maps found"));
0105     mapListBox->setEnabled(false);
0106   } else {
0107     for (int i = 0; i < maps.count(); i++) {
0108       mapListBox->addItem(maps.at(i)->name);
0109     }
0110     qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog: before connect ";
0111     // UNSOLVED CRASH: connect(mapListBox, SIGNAL(currentRowChanged(int)), this, SLOT(slotMapChanged(int)));
0112   }
0113 
0114   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::ImageMapChooseDialog: before call initImageListTable ";
0115   initImageListTable(layout);
0116 
0117   if (! maps.isEmpty()) {
0118     mapListBox->setCurrentItem(nullptr);
0119     slotMapChanged(0);
0120   }
0121 
0122   QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);
0123   QPushButton *okButton = buttonBox->button(QDialogButtonBox::Ok);
0124   okButton->setDefault(true);
0125   okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0126   connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
0127   connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
0128   okButton->setDefault(true);
0129   layout->addWidget(buttonBox);
0130 
0131   resize(510,460);
0132 }
0133 
0134 void ImageMapChooseDialog::initImageListTable(QLayout* layout) {
0135   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::initImageListTable ";
0136 
0137 
0138   if (images.isEmpty()) {
0139     imageListTable = new QTableWidget(1, 1);
0140     imageListTable->setItem(0,0, new QTableWidgetItem(i18n("No images found")));
0141     imageListTable->setEnabled(false);
0142     imageListTable->horizontalHeader()->hide();
0143     // PORT: imageListTable->setTopMargin(0);
0144     // PORT: imageListTable->setColumnStretchable(0,true);
0145   } else {
0146     imageListTable = new QTableWidget(images.count(), 2);
0147     // PORT: imageListTable->setColumnStretchable(0,true);
0148   }
0149 
0150   imageListTable->verticalHeader()->hide();
0151   // PORT imageListTable->setLeftMargin(0);
0152 
0153   QLabel *lbl = new QLabel(i18n("&Images"));
0154   lbl->setBuddy(imageListTable);
0155 
0156   layout->addWidget(lbl);
0157   layout->addWidget(imageListTable);
0158 
0159   if (images.isEmpty())
0160     return;
0161 
0162   imageListTable->setHorizontalHeaderLabels(QStringList() << i18n("Path") << "usemap");
0163 
0164   imageListTable->setSelectionMode(QAbstractItemView::SingleSelection);
0165   // PORT:  imageListTable->setFocusStyle(QTableWidget::FollowStyle);
0166   imageListTable->clearSelection();
0167 
0168 
0169   int row=0;
0170   QListIterator<ImageTag*> it(images);
0171   while (it.hasNext()) {
0172     QString src="";
0173     QString usemap="";
0174     ImageTag* tag = it.next();
0175     if (tag->contains("src"))
0176       src=tag->value("src");
0177     if (tag->contains("usemap"))
0178       usemap=tag->value("usemap");
0179 
0180     imageListTable->setItem(row,0, new QTableWidgetItem(src));
0181     imageListTable->setItem(row,1, new QTableWidgetItem(usemap));
0182     row++;
0183   }
0184 
0185   // UNSOLVED CRASH: connect (imageListTable, SIGNAL(itemSelectionChanged()), 
0186   // this, SLOT(slotImageChanged()));
0187 
0188   imageListTable->selectRow(0);
0189   slotImageChanged();
0190 
0191 
0192 }
0193 
0194 ImageMapChooseDialog::~ImageMapChooseDialog() {
0195 }
0196 
0197 void ImageMapChooseDialog::slotImageChanged()
0198 {
0199   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::slotImageChanged";
0200   int i=imageListTable->currentRow();
0201   if (i < 0 || i > images.count()) 
0202     i = 0;
0203   QImage pix;
0204   if (images.at(i)->contains("src")) {
0205     QString str = images.at(i)->value("src");
0206     // relative url
0207     if (baseUrl.path().isEmpty() | !baseUrl.path().endsWith('/')) {
0208         pixUrl=QUrl(baseUrl.path() + '/').resolved(QUrl(str));
0209     }
0210     else {
0211         pixUrl=baseUrl.resolved(QUrl(str));
0212     }
0213     pix=QImage(pixUrl.path());
0214     double zoom1=1;
0215     double zoom2=1;
0216     if (pix.width()>300)
0217       zoom1=(double) 300/pix.width();
0218     if (pix.height()>200)
0219       zoom2=(double) 200/pix.height();
0220 
0221 
0222     zoom1= zoom1 < zoom2 ? zoom1 : zoom2;
0223     pix=pix.scaled((int)(pix.width()*zoom1),
0224            int(pix.height()*zoom1),
0225            Qt::KeepAspectRatio,
0226            Qt::SmoothTransformation);
0227   }
0228   QPixmap pix2;
0229   pix2.fromImage(pix);
0230   imagePreview->setPixmap(pix2);
0231 
0232 //  imagePreview->repaint();
0233 }
0234 
0235 
0236 void ImageMapChooseDialog::selectImageWithUsemap(const QString & usemap) {
0237   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::selectImageWithUsemap: " << usemap;
0238 
0239   for (int i=0; i<imageListTable->rowCount(); i++) {
0240     QTableWidgetItem *item = imageListTable->item(i,1);
0241     if (item && (item->text()==usemap)) {
0242       imageListTable->selectRow(i);
0243       slotImageChanged();
0244       return;
0245     }
0246   }
0247 }
0248 
0249 void ImageMapChooseDialog::slotMapChanged(int i) {
0250   qCDebug(KIMAGEMAPEDITOR_LOG) << "ImageMapChooseDialog::slotMapChanged: " << i;
0251   currentMap=maps.at(i);
0252   selectImageWithUsemap(currentMap->name);
0253 }
0254