Back to index

texmacs  1.0.7.15
QTMFileDialog.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : QTMFileDialog.cpp
00004 * DESCRIPTION: QT file choosers
00005 * COPYRIGHT  : (C) 2009 David MICHEL
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "QTMFileDialog.hpp"
00013 #include <QPainter>
00014 #include <QLineEdit>
00015 #include <QIntValidator>
00016 #include <QMimeData>
00017 #include <QUrl>
00018 #include <QDrag>
00019 #include <QDragEnterEvent>
00020 #include <QDragLeaveEvent>
00021 #include <QDropEvent>
00022 #include "file.hpp"
00023 #include "sys_utils.hpp"
00024 #include "qt_utilities.hpp"
00025 #include "analyze.hpp"
00026 #include "dictionary.hpp"
00027 #include "image_files.hpp"
00028 
00029 QTMFileDialog::QTMFileDialog (QWidget * parent, const QString & caption, const QString & directory, const QString & filter)
00030   : QDialog (parent) {
00031   setWindowTitle (caption);
00032   hbox= new QHBoxLayout (this);
00033   hbox->setContentsMargins (0, 0, 0, 0);
00034   file= new QMyFileDialog (0, caption, directory, filter);
00035   hbox->addWidget (file);
00036   setLayout (hbox);
00037   setAcceptDrops(true);
00038   connect(file, SIGNAL(accepted()), this, SLOT(accept()));
00039   connect(file, SIGNAL(finished(int)), this, SLOT(done(int)));
00040   connect(file, SIGNAL(rejected()), this, SLOT(reject()));
00041 }
00042 
00043 void QTMFileDialog::dragEnterEvent(QDragEnterEvent *event)
00044 {
00045        event->acceptProposedAction();
00046 }
00047 
00048 void QTMFileDialog::dragMoveEvent(QDragMoveEvent *event)
00049 {
00050        event->acceptProposedAction();
00051 }
00052 
00053 void QTMFileDialog::dropEvent(QDropEvent *event)
00054 {
00055        const QMimeData *mimeData = event->mimeData();
00056        
00057        foreach (QString format, mimeData->formats()) {
00058               if (format == "text/uri-list") {
00059                      file->selectFile(mimeData->urls().at(0).toLocalFile());
00060                      break;
00061               }
00062        }
00063        
00064        event->acceptProposedAction();
00065 }
00066 
00067 void QTMFileDialog::dragLeaveEvent(QDragLeaveEvent *event)
00068 {
00069        event->accept();
00070 }
00071 
00072 
00073 static QWidget*
00074 simple_input (string s, QLineEdit* ledit, QWidget* parent= 0) {
00075   QWidget* widget= new QWidget (parent);
00076   QHBoxLayout* layout= new QHBoxLayout (widget);
00077   layout->setContentsMargins (0, 0, 0, 0);
00078 //  string in_lan= get_input_language ();
00079 //  string out_lan= get_output_language ();
00080 //  QLabel* label= new QLabel (to_qstring (tm_var_encode (translate (s, in_lan, out_lan))), parent);
00081   QLabel* label= new QLabel (to_qstring (qt_translate (s)), parent);
00082   layout->addWidget (label);
00083   layout->addWidget (ledit);
00084   widget->setLayout (layout);
00085   return widget;
00086 }
00087 
00088 QTMImagePreview::QTMImagePreview (QWidget* parent)
00089   : QWidget (parent) {
00090   QVBoxLayout* vbox= new QVBoxLayout (this);
00091   vbox->addStretch ();
00092   image= new QLabel (this);
00093   image->setMinimumWidth (100);
00094   image->setAlignment (Qt::AlignCenter);
00095   vbox->addWidget (image);
00096   vbox->addSpacing (10);
00097   wid= new QLineEdit (this);
00098   vbox->addWidget (simple_input ("Width:", wid, this));
00099   hei= new QLineEdit (this);
00100   vbox->addWidget (simple_input ("Height:", hei, this));
00101   xps= new QLineEdit (this);
00102   vbox->addWidget (simple_input ("X-position:", xps, this));
00103   yps= new QLineEdit (this);
00104   vbox->addWidget (simple_input ("Y-position:", yps, this));
00105   vbox->addStretch ();
00106   vbox->addStretch ();
00107   setLayout (vbox);
00108   setMinimumWidth (175);
00109   setMaximumWidth (225);
00110   setImage (0);
00111 }
00112 
00113 void
00114 QTMImagePreview::setImage (const QString& file) {
00115   QImage img;
00116   wid->setText ("");
00117   hei->setText ("");
00118   xps->setText ("");
00119   yps->setText ("");
00120 
00121   if (file.endsWith (".ps") ||
00122       file.endsWith (".eps") ||
00123       file.endsWith (".pdf")) {
00124     url temp= url_temp (".png");
00125     url image_url= url_system (scm_unquote (from_qstring_utf8 (file)));
00126     int w_pt, h_pt;
00127     double w, h;
00128     image_size (image_url, w_pt, h_pt);
00129     if (w_pt > h_pt) {
00130       w= 98;
00131       h= h_pt*98/w_pt;
00132       if ((int)h < h) h= (int)h+1; else h= (int)h;
00133     }
00134     else {
00135       w= w_pt*98/h_pt;
00136       if ((int)w < w) w= (int)w+1; else w= (int)w;
00137       h= 98;
00138     }
00139     image_to_png (image_url, temp, w, h);
00140     img.load (utf8_to_qstring (as_string (temp)));
00141     remove (temp);
00142   }
00143   else {
00144     img.load (file);
00145     if (!img.isNull()) {
00146       wid->setText (QString::number (img.width ()) + "px");
00147       hei->setText (QString::number (img.height ()) + "px");
00148     }
00149   }
00150 
00151   if (img.isNull()) {
00152     QImage vide (100, 100, QImage::Format_RGB32);
00153     QPainter painter;
00154     painter.begin (&vide);
00155     painter.fillRect (0, 0, 100, 100, Qt::white);
00156     QPen ThinBlack (Qt::black);
00157     ThinBlack.setWidth (0);
00158     ThinBlack.setStyle (Qt::SolidLine);
00159     painter.setPen (ThinBlack);
00160     painter.drawLine (0, 0, 99, 99);
00161     painter.drawLine (0, 99, 99, 0);
00162     painter.drawRect (0, 0, 99, 99);
00163     painter.end ();
00164     image->setPixmap(QPixmap::fromImage(vide));
00165   }
00166   else
00167     image->setPixmap (QPixmap::fromImage (img.scaled (98, 98, Qt::KeepAspectRatio, Qt::FastTransformation)));
00168 }
00169 
00170 QTMImageDialog::QTMImageDialog (QWidget* parent, const QString& caption, const QString& directory, const QString& filter)
00171   : QTMFileDialog (parent, caption, directory, filter)
00172 {
00173   preview= new QTMImagePreview (this);
00174   hbox->insertWidget(0, preview);
00175   connect(file, SIGNAL(currentChanged (const QString&)), preview, SLOT(setImage(const QString&)));
00176 }
00177 
00178 string
00179 QTMImageDialog::getParamsAsString () {
00180   string params;
00181   params << "\"" << from_qstring (preview->wid->text ()) << "\" ";
00182   params << "\"" << from_qstring (preview->hei->text ()) << "\" ";
00183   params << "\"" << from_qstring (preview->xps->text ()) << "\" ";
00184   params << "\"" << from_qstring (preview->yps->text ()) << "\"";
00185   return params;
00186 }