summaryrefslogtreecommitdiffstats
path: root/utils/wpseditor/gui/src/qwpsdrawer.cpp
blob: a506d5c03b8f08111069fdae0308249121bd9ef8 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <stdarg.h>

#include <QtGui>
#include <QLibrary>

#include "qwpsdrawer.h"
#include "slider.h"
#include "utils.h"
#include "qtrackstate.h"
#include "qwpsstate.h"
#include "api.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),mCurTarget("h10_5gb") {

    tryResolve();
    newTempWps();
}

bool QWpsDrawer::tryResolve() {
    QLibrary lib(qApp->applicationDirPath()+"/libwps_"+mCurTarget);
    lib_wps_init = (pfwps_init)lib.resolve("wps_init");
    lib_wps_display = (pfwps_display)lib.resolve("wps_display");
    lib_wps_refresh = (pfwps_refresh)lib.resolve("wps_refresh");
    lib_get_model_name = (pfget_model_name)lib.resolve("get_model_name");
    mResolved = lib_wps_init && lib_wps_display && lib_wps_refresh && lib_get_model_name;
    if (!mResolved)
        DEBUGF1(tr("ERR: Failed to resolve funcs!"));
    else {
        int v = api.verbose;
        memset(&api,0,sizeof(struct proxy_api));
        api.verbose = v;
        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;
        qDebug()<<(qApp->applicationDirPath()+"/libwps_"+mCurTarget+" resolved");
    }
    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;
            DEBUGF3(QString("Created :"+mTmpWpsString).toAscii());
        }
    }
}

void QWpsDrawer::WpsInit(QString buffer, bool isFile) {
	DEBUGF3("QWpsDrawer::WpsInit");
    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());
    }
    backdrop.fill(Qt::black);
    DEBUGF3("clear backdrop");
    if (isFile)
        lib_wps_init(buffer.toAscii(), &api, isFile);
    else
        lib_wps_init(QString(mTmpWpsString+".wps").toAscii(), &api, true);
    pix = new QPixmap(api.getwidth(),api.getheight());
    pix->fill(Qt::black);

    drawBackdrop();

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

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

    drawBackdrop();
    lib_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() {
    DEBUGF3("QWpsDrawer::drawBackdrop()");
    if (backdrop.isNull())
    	return;
    QPainter b(pix);
    QImage pink = backdrop.createMaskFromColor(qRgb(255,0,255),Qt::MaskOutColor);
    backdrop.setAlphaChannel(pink);
    b.drawImage(0,0,backdrop,0,0,pix->width(),pix->height());
}

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();
}

QString QWpsDrawer::getModelName(QString libraryName) {
    QLibrary lib(libraryName);
    if ((pfget_model_name)lib.resolve("get_model_name"))
        return ((pfget_model_name)lib.resolve("get_model_name"))();
    DEBUGF1("ERR: failed to resolve <get_model_name>");
    return "unknown";
}

QList<QString> QWpsDrawer::getTargets() {
    QList<QString> list ;
    QDir d = QDir(qApp->applicationDirPath());
    QFileInfoList libs = d.entryInfoList(QStringList("libwps*"));
    qDebug() << libs.size()<<"libs found";
    for (int i = 0; i < libs.size(); i++) {
        QString modelName = getModelName(libs[i].absoluteFilePath());
        qDebug() << libs[i].fileName()<<modelName;
        if (modelName == "unknown")
            continue;
        list.append(modelName);
    }
    return list;
}
bool QWpsDrawer::setTarget(QString target) {
    QLibrary lib(qApp->applicationDirPath()+"/libwps_"+mCurTarget);
    //lib.unload();
    if (getModelName("libwps_"+target)!="unknown") {
        mCurTarget = target;
        return tryResolve();
    }
    return false;
}