]> Shamusworld >> Repos - ttedit/blob - src/previewfiledialog.cpp
Added preview window to file loading dialog. :-)
[ttedit] / src / previewfiledialog.cpp
1 #include "previewfiledialog.h"
2 #include <QLabel>
3 #include <QGridLayout>
4 #include <QPainterPath>
5 #include <QPainter>
6 #include "charwindow.h"
7 #include "global.h"
8 #include "glyphpoints.h"
9
10
11 PreviewFileDialog::PreviewFileDialog(
12         QWidget * parent,
13         const QString & caption,
14         const QString & directory,
15         const QString & filter):
16     QFileDialog(parent, caption, directory, filter)
17 {
18         setObjectName("PreviewFileDialog");
19         QVBoxLayout * box = new QVBoxLayout;
20  
21         mpPreview = new QLabel(tr("Preview"), this);
22         mpPreview->setAlignment(Qt::AlignCenter);
23         mpPreview->setObjectName("labelPreview");
24         box->addWidget(mpPreview);
25  
26         box->addStretch();
27  
28         // add to QFileDialog layout
29         {
30                 QGridLayout * layout = (QGridLayout *)this->layout();
31                 layout->addLayout(box, 1, 3, 3, 1);
32         }
33
34         connect(this, SIGNAL(currentChanged(const QString &)), this, SLOT(HandleCurrentChanged(const QString &)));
35 }
36
37
38 void PreviewFileDialog::HandleCurrentChanged(const QString & path)
39 {
40         if (path.endsWith(".glyph") == false)
41                 return;
42
43         GlyphPoints gp;
44         QPainterPath * glyphPath = NULL;
45         FILE * fp = fopen(path.toUtf8().data(), "r");
46
47         if (fp != NULL)
48         {
49                 if (gp.LoadGlyphFromFile(fp) == true)
50                         glyphPath = Global::charWnd->MakePathFromPoints(&gp);
51
52                 fclose(fp);
53         }
54
55         QImage glyphImg(160, 160, QImage::Format_RGBA8888);
56         glyphImg.fill(Qt::transparent);
57         Global::charWnd->RenderPathInImage(glyphPath, &glyphImg);
58         QPixmap pixmap = QPixmap::fromImage(glyphImg);
59
60         if (pixmap.isNull() == false)
61         {
62                 mpPreview->setPixmap(pixmap);//.scaled(mpPreview->width(), mpPreview->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
63 //              return;
64         }
65
66 //      mpPreview->setText(QString("not an image"));//<br>%1").arg(path));
67 }
68