Back to index

texmacs  1.0.7.15
qt_chooser_widget.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003  * MODULE     : qt_chooser_widget.cpp
00004  * DESCRIPTION: File chooser widget, QT version
00005  * COPYRIGHT  : (C) 2008  Massimiliano Gubinelli
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 "qt_chooser_widget.hpp"
00013 #include "qt_utilities.hpp"
00014 #include "widget.hpp"
00015 #include "message.hpp"
00016 #include "analyze.hpp"
00017 #include "converter.hpp"
00018 #include "QTMFileDialog.hpp"
00019 
00020 #include <QtGui>
00021 
00028 qt_chooser_widget_rep::qt_chooser_widget_rep (command _cmd, string _type, bool _save)
00029  : qt_widget_rep (), cmd (_cmd), type (_type), save (_save), 
00030    position (coord2 (0, 0)), size (coord2 (100, 100)), file ("")
00031 {
00032   if (DEBUG_QT)
00033     cout << "qt_chooser_widget_rep::qt_chooser_widget_rep type=\""
00034     << type << "\" save=\"" << save << "\"" << LF;
00035 }
00036 
00037 qt_chooser_widget_rep::~qt_chooser_widget_rep() {}
00038 
00039 
00040 void
00041 qt_chooser_widget_rep::send (slot s, blackbox val) {
00042   if (DEBUG_QT)
00043     cout << "qt_chooser_widget_rep::send " << slot_name(s) << LF;
00044   switch (s) {
00045     case SLOT_VISIBILITY:
00046     {   
00047       check_type<bool> (val, "SLOT_VISIBILITY");
00048       bool flag = open_box<bool> (val);
00049       (void) flag;
00050       NOT_IMPLEMENTED
00051     }
00052       break;
00053     case SLOT_SIZE:
00054       TYPE_CHECK (type_box (val) == type_helper<coord2>::id);
00055       size = open_box<coord2> (val);
00056       break;
00057     case SLOT_POSITION:
00058       TYPE_CHECK (type_box (val) == type_helper<coord2>::id);
00059       position = open_box<coord2> (val);
00060       break;
00061     case SLOT_KEYBOARD_FOCUS:
00062       TYPE_CHECK (type_box (val) == type_helper<bool>::id);
00063       perform_dialog ();
00064       break;              
00065     case SLOT_STRING_INPUT:
00066         // send_string (THIS, "input", val);
00067       TYPE_CHECK (type_box (val) == type_helper<string>::id);
00068       if (DEBUG_QT) cout << "string input: " << open_box<string> (val) << LF;
00069       NOT_IMPLEMENTED
00070       break;
00071     case SLOT_INPUT_TYPE:
00072       TYPE_CHECK (type_box (val) == type_helper<string>::id);
00073       type = open_box<string> (val);
00074       break;
00075 #if 0
00076     case SLOT_INPUT_PROPOSAL:
00077         //send_string (THIS, "default", val);
00078       NOT_IMPLEMENTED
00079       break;
00080 #endif
00081     case SLOT_FILE:
00082         //send_string (THIS, "file", val);
00083       TYPE_CHECK (type_box (val) == type_helper<string>::id);
00084       if (DEBUG_QT) cout << "file: " << open_box<string> (val) << LF;
00085       file = open_box<string> (val);
00086       break;
00087     case SLOT_DIRECTORY:
00088       TYPE_CHECK (type_box (val) == type_helper<string>::id);
00089       directory = open_box<string> (val);
00090       directory = as_string (url_pwd () * url_system (directory));
00091       break;
00092       
00093     default:
00094       qt_widget_rep::send (s, val);
00095   }
00096 }
00097 
00098 blackbox
00099 qt_chooser_widget_rep::query (slot s, int type_id) {
00100   if (DEBUG_QT)
00101     cout << "qt_chooser_widget_rep::query " << slot_name(s) << LF;
00102   switch (s) {
00103     case SLOT_POSITION:
00104     {
00105       typedef pair<SI,SI> coord2;
00106       TYPE_CHECK (type_id == type_helper<coord2>::id);
00107       return close_box<coord2> (position);
00108     }
00109     case SLOT_SIZE:
00110     {
00111       typedef pair<SI,SI> coord2;
00112       TYPE_CHECK (type_id == type_helper<coord2>::id);
00113       return close_box<coord2> (size);
00114     }
00115     case SLOT_STRING_INPUT:
00116     {
00117       TYPE_CHECK (type_id == type_helper<string>::id);
00118       if (DEBUG_QT) cout << "String: " << file << LF;
00119       return close_box<string> (file);
00120     }
00121     default:
00122       return qt_widget_rep::query (s, type_id);
00123   }
00124 }
00125 
00126 void
00127 qt_chooser_widget_rep::notify (slot s, blackbox new_val) {
00128   if (DEBUG_QT)
00129     cout << "[qt_chooser_widget_rep ]";
00130   switch (s) {
00131     default: ;
00132   }
00133   qt_widget_rep::notify (s, new_val);
00134 }
00135 
00136 widget
00137 qt_chooser_widget_rep::read (slot s, blackbox index) {
00138   if (DEBUG_QT)
00139     cout << "qt_chooser_widget_rep::read " << slot_name(s) << LF;
00140   switch (s) {
00141     case SLOT_WINDOW:
00142       check_type_void (index, "SLOT_WINDOW");
00143       return this;
00144     case SLOT_FORM_FIELD:
00145       check_type<int> (index, "SLOT_FORM_FIELD");
00146       return this;
00147     case SLOT_FILE:
00148       check_type_void (index, "SLOT_FILE");
00149       return this;
00150     case SLOT_DIRECTORY:
00151       check_type_void (index, "SLOT_DIRECTORY");
00152       return this;
00153     default:
00154       return qt_widget_rep::read(s,index);
00155   }
00156 }
00157 
00158 void
00159 qt_chooser_widget_rep::write (slot s, blackbox index, widget w) {
00160   if (DEBUG_QT)
00161     cout << "[qt_chooser_widget] ";
00162   switch (s) {
00163     default:
00164       qt_widget_rep::write(s,index,w);
00165   }
00166 }
00167 
00168 widget
00169 qt_chooser_widget_rep::plain_window_widget (string s, command q)
00170 {
00171   // Identify ourselves as already being a top-level window, even though
00172   // the dialog won't be displayed until later.
00173   //view->setProperty ("texmacs_window_widget",
00174   //                 QVariant::fromValue ((void*) this));
00175   win_title = s;
00176   quit = q;       // (NOT USED)
00177   return this;
00178 }
00179 
00180 #if (defined(Q_WS_MAC) || defined(Q_WS_WIN))
00181 
00187 void
00188 qt_chooser_widget_rep::perform_dialog () {
00189   QString _file;
00190   QStringList _files;
00191   QString _filter, _suffix, _caption, _directory;
00192   _caption = to_qstring (win_title);
00193   _directory = QString::fromLocal8Bit (as_charp (directory * "/" * file));
00194   
00195 #if (QT_VERSION >= 0x040400)
00196   if (type == "directory") {
00197   } else if (type == "texmacs") {
00198     _filter= "TeXmacs file (*.tm *.ts *.tp)";
00199     _suffix= "tm";
00200   } else if (type == "image") {
00201     _filter= "Image file (*.gif *.jpg *.jpeg *.pdf *.png *.pnm *.ps *.eps *.ppm *.svg *.tif *.fig *.xpm)";
00202   } else if (type == "bibtex") {
00203     _filter= "BibTeX file (*.bib)";
00204     _suffix= "bib";
00205   } else if (type == "html") {
00206     _filter= "Html file (*.html *.xhtml *.htm)";
00207     _suffix= "html";
00208   } else if (type == "latex") {
00209     _filter= "LaTeX file (*.tex *.ltx *.sty *.cls)";
00210     _suffix= "tex";
00211   } else if (type == "stm") {
00212     _filter= "Scheme file (*.stm *.scm)";
00213     _suffix= "stm";
00214   } else if (type == "verbatim") {
00215     _filter= "Verbatim file (*.txt)";
00216     _suffix= "txt";
00217   } else if (type == "tmml") {
00218     _filter= "XML file (*.tmml)";
00219     _suffix= "tmml";  
00220   } else if (type == "pdf") {
00221     _filter= "Pdf file (*.pdf)";
00222     _suffix= "pdf";
00223   } else if (type == "postscript") {
00224     _filter= "PostScript file (*.ps *.eps)";
00225     _suffix= "ps";  
00226   }
00227 #endif
00228   
00229   if (type == "directory")
00230     _file = QFileDialog::getExistingDirectory (0, _caption, _directory);
00231   else if (save)
00232     _file = QFileDialog::getSaveFileName(0, _caption, _directory, _filter);
00233   else
00234     _file = QFileDialog::getOpenFileName (0, _caption, _directory, _filter);
00235   
00236   if (_file.isEmpty()) {
00237     file= "#f";
00238   } else {
00239     url u = url_system (scm_unquote (from_qstring (_file)));
00240     QByteArray arr= to_qstring (as_string (u)).toLocal8Bit ();
00241     const char* cstr= arr.constData ();
00242     string localname = string ((char*) cstr);
00243     if (type == "image") {
00244       /*
00245        QPixmap _pic(_file);
00246        string params;
00247        // TEMPORARY HACK: wouldn't it be nicer to scale the image to, say, 
00248        // 90% of the current column width?
00249        params << "\"" << from_qstring(QString("%1px").arg(_pic.width())) << "\" ";
00250        params << "\"" << from_qstring(QString("%1px").arg(_pic.height())) << "\" ";
00251        params << "\"" << "" << "\" ";  // xps ??
00252        params << "\"" << "" << "\"";   // yps ??
00253        file = "(list (url-system " * scm_quote (as_string (u)) * ") " * params * ")";
00254        */
00255       file = "(list (url-system " * scm_quote (localname) * ") \"\" \"\" \"\" \"\")";
00256     } else {
00257       file = "(url-system " * scm_quote (localname) * ")";
00258     }
00259   }
00260   cmd ();
00261   if (!is_nil(quit)) quit ();
00262 }
00263 #else
00264 
00268 void
00269 qt_chooser_widget_rep::perform_dialog () {
00270     // int result;
00271   
00272   QTMFileDialog *dialog;
00273   QTMImageDialog *imgdialog= 0; // to avoid a dynamic_cast
00274   
00275   if (type  == "image")
00276     dialog= imgdialog= new QTMImageDialog (NULL, to_qstring (win_title), to_qstring(directory * "/" * file));
00277   else
00278     dialog= new QTMFileDialog (NULL, to_qstring (win_title), to_qstring(directory * "/" * file));
00279   
00280 #if (defined(Q_WS_MAC) && (QT_VERSION >= 0x040500))
00281   dialog->setOptions(QFileDialog::DontUseNativeDialog);
00282 #endif
00283   
00284   QPoint pos = to_qpoint(position);
00285     //cout << "Size :" << size.x1 << "," << size.x2 << LF;
00286   if (DEBUG_QT) {
00287     cout << "Position :" << pos.x() << "," << pos.y() << LF;
00288     cout << "Dir: " << directory * "/" * file << LF;
00289   }
00290   
00291   dialog->updateGeometry();
00292   QSize sz = dialog->sizeHint();
00293   QRect r; r.setSize(sz);
00294   r.moveCenter(pos);
00295   dialog->setGeometry(r);
00296   
00297   dialog->setViewMode (QFileDialog::Detail);
00298   if (type == "directory") {
00299     dialog->setFileMode(QFileDialog::Directory);
00300   } else if (type == "image") {
00301     dialog->setFileMode(QFileDialog::ExistingFile);
00302   } else {
00303     dialog->setFileMode(QFileDialog::AnyFile);
00304   }
00305   
00306 #if (QT_VERSION >= 0x040400)
00307   if (type == "directory") {  
00308   } else if (type == "texmacs") {
00309     dialog->setNameFilter ("TeXmacs file (*.tm *.ts *.tp)");
00310     dialog->setDefaultSuffix ("tm");
00311   } else if (type == "image") {
00312     dialog->setNameFilter ("Image file (*.gif *.jpg *.jpeg *.pdf *.png *.pnm *.ps *.eps *.ppm *.svg *.tif *.tiff *.fig *.xpm)");
00313   } else if (type == "bibtex") {
00314     dialog->setNameFilter ("BibTeX file (*.bib)");
00315     dialog->setDefaultSuffix ("bib");
00316   } else if (type == "html") {
00317     dialog->setNameFilter ("Html file (*.htm *.html *.xhtml)");
00318     dialog->setDefaultSuffix ("html");
00319   } else if (type == "latex") {
00320     dialog->setNameFilter ("LaTeX file (*.tex *.ltx *.sty *.cls)");
00321     dialog->setDefaultSuffix ("tex");
00322   } else if (type == "stm") {
00323     dialog->setNameFilter ("Scheme file (*.stm *.scm)");
00324     dialog->setDefaultSuffix ("stm");
00325   } else if (type == "verbatim") {
00326     dialog->setNameFilter ("Verbatim file (*.txt)");
00327     dialog->setDefaultSuffix ("txt");
00328   } else if (type == "tmml") {
00329     dialog->setNameFilter ("XML file (*.tmml)");
00330     dialog->setDefaultSuffix ("tmml");  
00331   } else if (type == "pdf") {
00332     dialog->setNameFilter ("Pdf file (*.pdf)");
00333     dialog->setDefaultSuffix ("pdf");
00334   } else if (type == "postscript") {
00335     dialog->setNameFilter ("PostScript file (*.ps *.eps)");
00336     dialog->setDefaultSuffix ("ps");  
00337   }
00338 #endif
00339   
00340   dialog->setLabelText(QFileDialog::Accept, "Ok");
00341   
00342   QStringList fileNames;
00343   if (dialog->exec ()) {
00344     fileNames = dialog->selectedFiles();
00345     if (fileNames.count() > 0) {
00346       file = from_qstring (fileNames[0]);
00347       url u = url_system (scm_unquote (file));
00348       QByteArray arr= to_qstring (as_string (u)).toLocal8Bit ();
00349       const char* cstr= arr.constData ();
00350       string localname = string ((char*) cstr);
00351       if (type == "image")
00352         file = "(list (url-system " *
00353         scm_quote (as_string (u)) *
00354         ") " * imgdialog->getParamsAsString () * ")";
00355       else
00356         file = "(url-system " * scm_quote (localname) * ")";
00357     }
00358   } else {
00359     file = "#f";
00360   }
00361   
00362   delete dialog;
00363   
00364   cmd ();
00365   if (!is_nil(quit)) quit ();
00366 }
00367 #endif