summaryrefslogtreecommitdiffstats
path: root/utils/wpseditor/gui/src/qwpsdrawer.cpp
blob: ab8a4b32d65dfdb18d7525f1feaf3e8419610e8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "qwpsdrawer.h"
#include "slider.h"
#include "utils.h"
#include <QtGui>
#include <QLibrary>
#include <stdarg.h>
//


QPointer<QWpsDrawer> drawer;
QPixmap   *QWpsDrawer::pix = NULL;
QString   QWpsDrawer::mTmpWpsString;
QImage    QWpsDrawer::backdrop;
proxy_api QWpsDrawer::api;

QWpsDrawer::QWpsDrawer( QWpsState *ws,QTrackState *ms, QWidget *parent )
        : QWidget(parent),wpsState(ws),trackState(ms),showGrid(false),mTargetLibName("libwps") {

    tryResolve();
    memset(&api,0,sizeof(struct proxy_api));

    api.verbose = 2;
    api.putsxy =                    &QWpsDrawer::putsxy;
    api.transparent_bitmap_part =   &QWpsDrawer::transparent_bitmap_part;
    api.bitmap_part =               &QWpsDrawer::bitmap_part;
    api.drawpixel =                 &QWpsDrawer::drawpixel;
    api.fillrect =                  &QWpsDrawer::fillrect;
    api.hline =                     &QWpsDrawer::hline;
    api.vline =                     &QWpsDrawer::vline;
    api.clear_viewport =            &QWpsDrawer::clear_viewport;
    api.load_wps_backdrop =         &QWpsDrawer::load_wps_backdrop;
    api.read_bmp_file =             &QWpsDrawer::read_bmp_file;
    api.debugf =                    &qlogger;
    newTempWps();
}

bool QWpsDrawer::tryResolve() {
    QLibrary lib(qApp->applicationDirPath()+"/"+mTargetLibName);
    wps_init = (pfwps_init)lib.resolve("wps_init");
    wps_display = (pfwps_display)lib.resolve("wps_display");
    wps_refresh = (pfwps_refresh)lib.resolve("wps_refresh");
    mResolved = wps_init && wps_display && wps_refresh;
    if (!mResolved)
        DEBUGF1(tr("ERR: Failed to resolve funcs!"));
    return mResolved;
}
QWpsDrawer::~QWpsDrawer() {
    qDebug()<<"QWpsDrawer::~QWpsDrawer()";
    cleanTemp();
}

void QWpsDrawer::mouseReleaseEvent ( QMouseEvent * event ) {
    Q_UNUSED(event);
    /*int x = event->x() - (this->width()-pix->width())/2,
            y = event->y() - (this->height()-pix->height())/2;
    DEBUGF1("x=%d,y=%d",x,y);*/
}
void QWpsDrawer::newTempWps() {
    QTemporaryFile    tmpWps;
    tmpWps.setAutoRemove(false);
    tmpWps.setFileTemplate(QDir::tempPath()+"/XXXXXXXXXX.wps");
    if (tmpWps.open()) {
        QString tmpDir = tmpWps.fileName().left(tmpWps.fileName().length()-4);
        if (QDir::temp().mkpath(tmpDir)) {
            mTmpWpsString = tmpDir;
            DEBUGF1(mTmpWpsString);
        }
    }
}

void QWpsDrawer::WpsInit(QString buffer, bool isFile) {

    if (!mResolved)
        if (!tryResolve())
            return;
    if (isFile) {
        cleanTemp();
        DEBUGF1( tr("Loading %1").arg(buffer));
        QFile file(buffer);
        if (file.open(QIODevice::ReadOnly | QIODevice::Text))
            mWpsString = file.readAll();
        newTempWps();
    } else
        mWpsString = buffer;
    {
        QFile tfile(mTmpWpsString+".wps");
        if (tfile.open(QIODevice::WriteOnly | QIODevice::Text))
            tfile.write(mWpsString.toAscii(),mWpsString.length());
    }

    if (isFile)
        wps_init(buffer.toAscii(), &api, isFile);
    else
        wps_init(QString(mTmpWpsString+".wps").toAscii(), &api, true);
    pix = new QPixmap(api.getwidth(),api.getheight());

    drawBackdrop();

    setMinimumWidth(api.getwidth());
    setMinimumHeight(api.getheight());

    update();
}

void QWpsDrawer::paintEvent(QPaintEvent * event) {
    if (!mResolved)
        return;
    if (pix==NULL)
        return;
    QPainter p(this);
    QRect rect = event->rect();

    drawBackdrop();
    wps_refresh();

    if (showGrid) {
        QPainter g(pix);
        viewport_api avp;
        api.get_current_vp(&avp);

        g.setPen(Qt::green);

        for (int i=0;i*avp.fontheight/1.5<avp.width ;i++) {
            g.drawLine(int(i*avp.fontheight/1.5), 0, int(i*avp.fontheight/1.5), avp.height);
        }
        for (int j=0;j*avp.fontheight<avp.height; j++) {
            g.drawLine(0,j*avp.fontheight,avp.width,j*avp.fontheight);
        }
    }

    p.drawPixmap((rect.width()-pix->width())/2,(rect.height()-pix->height())/2,*pix);

}

void QWpsDrawer::clear_viewport(int x,int y,int w,int h, int color) {
    DEBUGF2("clear_viewport(int x=%d,int y=%d,int w=%d,int h=%d, int color)",x,y,w,h);
    QPainter p(pix);
    //p.setOpacity(0.1);
    //QImage img = backdrop.copy(x,y,w,h);
    //p.drawImage(x,y,img);
}

void QWpsDrawer::slotSetVolume() {
    Slider *slider = new Slider(this, tr("Volume"),-74,10);
    slider->show();
    connect(slider, SIGNAL(valueChanged(int)), wpsState, SLOT(setVolume(int)));
    connect(this, SIGNAL(destroyed()),slider, SLOT(close()));
}

void QWpsDrawer::slotSetProgress() {
    Slider *slider = new Slider(this,tr("Progress"),0,100);
    slider->show();
    connect(slider, SIGNAL(valueChanged(int)), trackState, SLOT(setElapsed(int)));
    connect(this, SIGNAL(destroyed()),slider, SLOT(close()));
}

void QWpsDrawer::slotWpsStateChanged(wpsstate ws_) {
    if (api.set_wpsstate)
        api.set_wpsstate(ws_);
    update();
}

void QWpsDrawer::slotTrackStateChanged(trackstate ms_) {
    if (api.set_wpsstate)
        api.set_trackstate(ms_);
    update();
}

void QWpsDrawer::slotShowGrid(bool show) {
    showGrid = show;
    update();
}

void QWpsDrawer::drawBackdrop() {
    QPainter b(pix);
    QImage pink = backdrop.createMaskFromColor(qRgb(255,0,255),Qt::MaskOutColor);
    backdrop.setAlphaChannel(pink);
    b.drawImage(0,0,backdrop);
}

void QWpsDrawer::slotSetAudioStatus(int status) {
    api.set_audio_status(status);
    update();
}

void QWpsDrawer::cleanTemp(bool fileToo) {
    if (fileToo)
        QFile::remove(mTmpWpsString+".wps");
    QDirIterator it(mTmpWpsString, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        QFile::remove(it.next());
    }
    QDir(mTmpWpsString).rmdir(mTmpWpsString);
}

void QWpsDrawer::closeEvent(QCloseEvent *event) {
    qDebug()<<"QWpsDrawer::closeEvent()";
    cleanTemp();
    event->accept();
}