"Fossies" - the Fresh Open Source Software Archive 
Member "TeXmacs-2.1.2-src/src/Plugins/Qt/QTMFileDialog.cpp" (5 May 2022, 5889 Bytes) of package /linux/misc/TeXmacs-2.1.2-src.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1
2 /******************************************************************************
3 * MODULE : QTMFileDialog.cpp
4 * DESCRIPTION: QT file choosers
5 * COPYRIGHT : (C) 2009 David MICHEL
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12 #include "QTMFileDialog.hpp"
13 #include <QPainter>
14 #include <QLineEdit>
15 #include <QIntValidator>
16 #include <QMimeData>
17 #include <QUrl>
18 #include <QDrag>
19 #include <QDragEnterEvent>
20 #include <QDragLeaveEvent>
21 #include <QDropEvent>
22 #include "file.hpp"
23 #include "sys_utils.hpp"
24 #include "qt_utilities.hpp"
25 #include "qt_gui.hpp"
26 #include "analyze.hpp"
27 #include "dictionary.hpp"
28 #include "image_files.hpp"
29
30 QTMFileDialog::QTMFileDialog (QWidget * parent, const QString & caption,
31 const QString & directory, const QString & filter)
32 : QDialog (parent)
33 {
34 setWindowTitle (caption);
35 hbox= new QHBoxLayout (this);
36 hbox->setContentsMargins (0, 0, 0, 0);
37 file= new QMyFileDialog (0, caption, directory, filter);
38 file->setOption(QFileDialog::DontUseNativeDialog, true);
39 hbox->addWidget (file);
40 setLayout (hbox);
41 setAcceptDrops(true);
42 connect(file, SIGNAL(accepted()), this, SLOT(accept()));
43 connect(file, SIGNAL(finished(int)), this, SLOT(done(int)));
44 connect(file, SIGNAL(rejected()), this, SLOT(reject()));
45 }
46
47 void QTMFileDialog::dragEnterEvent(QDragEnterEvent *event)
48 {
49 event->acceptProposedAction();
50 }
51
52 void QTMFileDialog::dragMoveEvent(QDragMoveEvent *event)
53 {
54 event->acceptProposedAction();
55 }
56
57 void QTMFileDialog::dropEvent(QDropEvent *event)
58 {
59 const QMimeData *mimeData = event->mimeData();
60
61 foreach (QString format, mimeData->formats()) {
62 if (format == "text/uri-list") {
63 file->selectFile(mimeData->urls().at(0).toLocalFile());
64 break;
65 }
66 }
67
68 event->acceptProposedAction();
69 }
70
71 void QTMFileDialog::dragLeaveEvent(QDragLeaveEvent *event)
72 {
73 event->accept();
74 }
75
76
77 static QWidget*
78 simple_input (string s, QLineEdit* ledit, QWidget* parent= 0) {
79 QWidget* widget= new QWidget (parent);
80 QHBoxLayout* layout= new QHBoxLayout (widget);
81 layout->setContentsMargins (0, 0, 0, 0);
82 // string in_lan= get_input_language ();
83 // string out_lan= get_output_language ();
84 // QLabel* label= new QLabel (to_qstring (tm_var_encode (translate (s, in_lan, out_lan))), parent);
85 QLabel* label= new QLabel (to_qstring (s), parent);
86 layout->addWidget (label);
87 layout->addWidget (ledit);
88 widget->setLayout (layout);
89 return widget;
90 }
91
92 QTMImagePreview::QTMImagePreview (QWidget* parent)
93 : QWidget (parent) {
94 QVBoxLayout* vbox= new QVBoxLayout (this);
95 vbox->addStretch ();
96 image= new QLabel (this);
97 image->setMinimumWidth (100);
98 image->setAlignment (Qt::AlignCenter);
99 vbox->addWidget (image);
100 vbox->addSpacing (10);
101 wid= new QLineEdit (this);
102 vbox->addWidget (simple_input ("Width:", wid, this));
103 hei= new QLineEdit (this);
104 vbox->addWidget (simple_input ("Height:", hei, this));
105 xps= new QLineEdit (this);
106 vbox->addWidget (simple_input ("X-position:", xps, this));
107 yps= new QLineEdit (this);
108 vbox->addWidget (simple_input ("Y-position:", yps, this));
109 vbox->addStretch ();
110 vbox->addStretch ();
111 setLayout (vbox);
112 setMinimumWidth (175);
113 setMaximumWidth (225);
114 setImage (0);
115 }
116
117 void
118 QTMImagePreview::setImage (const QString& file) { //generate thumbnail
119 BEGIN_SLOT
120 QImage img;
121 wid->setText ("");
122 hei->setText ("");
123 xps->setText ("");
124 yps->setText ("");
125
126 string localname= from_qstring_utf8 (file);
127 url image_url= url_system (localname);
128 if (DEBUG_CONVERT) debug_convert<<"image preview :["<<image_url<<"]"<<LF;
129 if (!(as_string(image_url)=="") && !is_directory(image_url) && exists(image_url) ){
130 url temp= url_temp (".png");
131 int w_pt, h_pt;
132 double w, h;
133 image_size (image_url, w_pt, h_pt);
134 if (w_pt*h_pt !=0) { //necessary if gs returns h=v=0 (for instance 0-size pdf)
135 wid->setText (QString::number (w_pt) + "pt");
136 hei->setText (QString::number (h_pt) + "pt");
137 if (w_pt > h_pt) {
138 w= 98;
139 h= ceil (h_pt*98/w_pt);
140 } else {
141 w= ceil (w_pt*98/h_pt);
142 h= 98;
143 }
144 // generate thumbnail:
145 image_to_png (image_url, temp, w, h);
146 img.load (utf8_to_qstring (as_string (temp)));
147 remove (temp);
148 }
149 }
150
151 if (img.isNull()) {
152 QImage vide (100, 100, QImage::Format_RGB32);
153 QPainter painter;
154 painter.begin (&vide);
155 painter.fillRect (0, 0, 100, 100, Qt::white);
156 QPen ThinBlack (Qt::black);
157 ThinBlack.setWidth (0);
158 ThinBlack.setStyle (Qt::SolidLine);
159 painter.setPen (ThinBlack);
160 painter.drawLine (0, 0, 99, 99);
161 painter.drawLine (0, 99, 99, 0);
162 painter.drawRect (0, 0, 99, 99);
163 painter.end ();
164 image->setPixmap(QPixmap::fromImage(vide));
165 }
166 else
167 image->setPixmap (QPixmap::fromImage (img.scaled (98, 98, Qt::KeepAspectRatio, Qt::FastTransformation)));
168 END_SLOT
169 }
170
171 QTMImageDialog::QTMImageDialog (QWidget* parent, const QString& caption, const QString& directory, const QString& filter)
172 : QTMFileDialog (parent, caption, directory, filter)
173 {
174 preview= new QTMImagePreview (this);
175 hbox->insertWidget(0, preview);
176 connect(file, SIGNAL(currentChanged (const QString&)), preview, SLOT(setImage(const QString&)));
177 }
178
179 string
180 QTMImageDialog::getParamsAsString () {
181 string params;
182 params << "\"" << from_qstring (preview->wid->text ()) << "\" ";
183 params << "\"" << from_qstring (preview->hei->text ()) << "\" ";
184 params << "\"" << from_qstring (preview->xps->text ()) << "\" ";
185 params << "\"" << from_qstring (preview->yps->text ()) << "\"";
186 return params;
187 }