diff options
Diffstat (limited to 'utils/wpseditor/gui/src/qwpsdrawer.cpp')
-rw-r--r-- | utils/wpseditor/gui/src/qwpsdrawer.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/qwpsdrawer.cpp b/utils/wpseditor/gui/src/qwpsdrawer.cpp new file mode 100644 index 0000000000..ab8a4b32d6 --- /dev/null +++ b/utils/wpseditor/gui/src/qwpsdrawer.cpp | |||
@@ -0,0 +1,200 @@ | |||
1 | #include "qwpsdrawer.h" | ||
2 | #include "slider.h" | ||
3 | #include "utils.h" | ||
4 | #include <QtGui> | ||
5 | #include <QLibrary> | ||
6 | #include <stdarg.h> | ||
7 | // | ||
8 | |||
9 | |||
10 | QPointer<QWpsDrawer> drawer; | ||
11 | QPixmap *QWpsDrawer::pix = NULL; | ||
12 | QString QWpsDrawer::mTmpWpsString; | ||
13 | QImage QWpsDrawer::backdrop; | ||
14 | proxy_api QWpsDrawer::api; | ||
15 | |||
16 | QWpsDrawer::QWpsDrawer( QWpsState *ws,QTrackState *ms, QWidget *parent ) | ||
17 | : QWidget(parent),wpsState(ws),trackState(ms),showGrid(false),mTargetLibName("libwps") { | ||
18 | |||
19 | tryResolve(); | ||
20 | memset(&api,0,sizeof(struct proxy_api)); | ||
21 | |||
22 | api.verbose = 2; | ||
23 | api.putsxy = &QWpsDrawer::putsxy; | ||
24 | api.transparent_bitmap_part = &QWpsDrawer::transparent_bitmap_part; | ||
25 | api.bitmap_part = &QWpsDrawer::bitmap_part; | ||
26 | api.drawpixel = &QWpsDrawer::drawpixel; | ||
27 | api.fillrect = &QWpsDrawer::fillrect; | ||
28 | api.hline = &QWpsDrawer::hline; | ||
29 | api.vline = &QWpsDrawer::vline; | ||
30 | api.clear_viewport = &QWpsDrawer::clear_viewport; | ||
31 | api.load_wps_backdrop = &QWpsDrawer::load_wps_backdrop; | ||
32 | api.read_bmp_file = &QWpsDrawer::read_bmp_file; | ||
33 | api.debugf = &qlogger; | ||
34 | newTempWps(); | ||
35 | } | ||
36 | |||
37 | bool QWpsDrawer::tryResolve() { | ||
38 | QLibrary lib(qApp->applicationDirPath()+"/"+mTargetLibName); | ||
39 | wps_init = (pfwps_init)lib.resolve("wps_init"); | ||
40 | wps_display = (pfwps_display)lib.resolve("wps_display"); | ||
41 | wps_refresh = (pfwps_refresh)lib.resolve("wps_refresh"); | ||
42 | mResolved = wps_init && wps_display && wps_refresh; | ||
43 | if (!mResolved) | ||
44 | DEBUGF1(tr("ERR: Failed to resolve funcs!")); | ||
45 | return mResolved; | ||
46 | } | ||
47 | QWpsDrawer::~QWpsDrawer() { | ||
48 | qDebug()<<"QWpsDrawer::~QWpsDrawer()"; | ||
49 | cleanTemp(); | ||
50 | } | ||
51 | |||
52 | void QWpsDrawer::mouseReleaseEvent ( QMouseEvent * event ) { | ||
53 | Q_UNUSED(event); | ||
54 | /*int x = event->x() - (this->width()-pix->width())/2, | ||
55 | y = event->y() - (this->height()-pix->height())/2; | ||
56 | DEBUGF1("x=%d,y=%d",x,y);*/ | ||
57 | } | ||
58 | void QWpsDrawer::newTempWps() { | ||
59 | QTemporaryFile tmpWps; | ||
60 | tmpWps.setAutoRemove(false); | ||
61 | tmpWps.setFileTemplate(QDir::tempPath()+"/XXXXXXXXXX.wps"); | ||
62 | if (tmpWps.open()) { | ||
63 | QString tmpDir = tmpWps.fileName().left(tmpWps.fileName().length()-4); | ||
64 | if (QDir::temp().mkpath(tmpDir)) { | ||
65 | mTmpWpsString = tmpDir; | ||
66 | DEBUGF1(mTmpWpsString); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | void QWpsDrawer::WpsInit(QString buffer, bool isFile) { | ||
72 | |||
73 | if (!mResolved) | ||
74 | if (!tryResolve()) | ||
75 | return; | ||
76 | if (isFile) { | ||
77 | cleanTemp(); | ||
78 | DEBUGF1( tr("Loading %1").arg(buffer)); | ||
79 | QFile file(buffer); | ||
80 | if (file.open(QIODevice::ReadOnly | QIODevice::Text)) | ||
81 | mWpsString = file.readAll(); | ||
82 | newTempWps(); | ||
83 | } else | ||
84 | mWpsString = buffer; | ||
85 | { | ||
86 | QFile tfile(mTmpWpsString+".wps"); | ||
87 | if (tfile.open(QIODevice::WriteOnly | QIODevice::Text)) | ||
88 | tfile.write(mWpsString.toAscii(),mWpsString.length()); | ||
89 | } | ||
90 | |||
91 | if (isFile) | ||
92 | wps_init(buffer.toAscii(), &api, isFile); | ||
93 | else | ||
94 | wps_init(QString(mTmpWpsString+".wps").toAscii(), &api, true); | ||
95 | pix = new QPixmap(api.getwidth(),api.getheight()); | ||
96 | |||
97 | drawBackdrop(); | ||
98 | |||
99 | setMinimumWidth(api.getwidth()); | ||
100 | setMinimumHeight(api.getheight()); | ||
101 | |||
102 | update(); | ||
103 | } | ||
104 | |||
105 | void QWpsDrawer::paintEvent(QPaintEvent * event) { | ||
106 | if (!mResolved) | ||
107 | return; | ||
108 | if (pix==NULL) | ||
109 | return; | ||
110 | QPainter p(this); | ||
111 | QRect rect = event->rect(); | ||
112 | |||
113 | drawBackdrop(); | ||
114 | wps_refresh(); | ||
115 | |||
116 | if (showGrid) { | ||
117 | QPainter g(pix); | ||
118 | viewport_api avp; | ||
119 | api.get_current_vp(&avp); | ||
120 | |||
121 | g.setPen(Qt::green); | ||
122 | |||
123 | for (int i=0;i*avp.fontheight/1.5<avp.width ;i++) { | ||
124 | g.drawLine(int(i*avp.fontheight/1.5), 0, int(i*avp.fontheight/1.5), avp.height); | ||
125 | } | ||
126 | for (int j=0;j*avp.fontheight<avp.height; j++) { | ||
127 | g.drawLine(0,j*avp.fontheight,avp.width,j*avp.fontheight); | ||
128 | } | ||
129 | } | ||
130 | |||
131 | p.drawPixmap((rect.width()-pix->width())/2,(rect.height()-pix->height())/2,*pix); | ||
132 | |||
133 | } | ||
134 | |||
135 | void QWpsDrawer::clear_viewport(int x,int y,int w,int h, int color) { | ||
136 | DEBUGF2("clear_viewport(int x=%d,int y=%d,int w=%d,int h=%d, int color)",x,y,w,h); | ||
137 | QPainter p(pix); | ||
138 | //p.setOpacity(0.1); | ||
139 | //QImage img = backdrop.copy(x,y,w,h); | ||
140 | //p.drawImage(x,y,img); | ||
141 | } | ||
142 | |||
143 | void QWpsDrawer::slotSetVolume() { | ||
144 | Slider *slider = new Slider(this, tr("Volume"),-74,10); | ||
145 | slider->show(); | ||
146 | connect(slider, SIGNAL(valueChanged(int)), wpsState, SLOT(setVolume(int))); | ||
147 | connect(this, SIGNAL(destroyed()),slider, SLOT(close())); | ||
148 | } | ||
149 | |||
150 | void QWpsDrawer::slotSetProgress() { | ||
151 | Slider *slider = new Slider(this,tr("Progress"),0,100); | ||
152 | slider->show(); | ||
153 | connect(slider, SIGNAL(valueChanged(int)), trackState, SLOT(setElapsed(int))); | ||
154 | connect(this, SIGNAL(destroyed()),slider, SLOT(close())); | ||
155 | } | ||
156 | |||
157 | void QWpsDrawer::slotWpsStateChanged(wpsstate ws_) { | ||
158 | if (api.set_wpsstate) | ||
159 | api.set_wpsstate(ws_); | ||
160 | update(); | ||
161 | } | ||
162 | |||
163 | void QWpsDrawer::slotTrackStateChanged(trackstate ms_) { | ||
164 | if (api.set_wpsstate) | ||
165 | api.set_trackstate(ms_); | ||
166 | update(); | ||
167 | } | ||
168 | |||
169 | void QWpsDrawer::slotShowGrid(bool show) { | ||
170 | showGrid = show; | ||
171 | update(); | ||
172 | } | ||
173 | |||
174 | void QWpsDrawer::drawBackdrop() { | ||
175 | QPainter b(pix); | ||
176 | QImage pink = backdrop.createMaskFromColor(qRgb(255,0,255),Qt::MaskOutColor); | ||
177 | backdrop.setAlphaChannel(pink); | ||
178 | b.drawImage(0,0,backdrop); | ||
179 | } | ||
180 | |||
181 | void QWpsDrawer::slotSetAudioStatus(int status) { | ||
182 | api.set_audio_status(status); | ||
183 | update(); | ||
184 | } | ||
185 | |||
186 | void QWpsDrawer::cleanTemp(bool fileToo) { | ||
187 | if (fileToo) | ||
188 | QFile::remove(mTmpWpsString+".wps"); | ||
189 | QDirIterator it(mTmpWpsString, QDirIterator::Subdirectories); | ||
190 | while (it.hasNext()) { | ||
191 | QFile::remove(it.next()); | ||
192 | } | ||
193 | QDir(mTmpWpsString).rmdir(mTmpWpsString); | ||
194 | } | ||
195 | |||
196 | void QWpsDrawer::closeEvent(QCloseEvent *event) { | ||
197 | qDebug()<<"QWpsDrawer::closeEvent()"; | ||
198 | cleanTemp(); | ||
199 | event->accept(); | ||
200 | } | ||