~l3on/ubuntu/precise/rkward/rebuild1

« back to all changes in this revision

Viewing changes to rkward/plugin/rkpreviewbox.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Friedrichsmeier
  • Date: 2008-04-20 21:30:00 UTC
  • mfrom: (1.2.2 upstream) (3.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080420213000-fs4i8efmfc793bnn
new upstream release
closes: #475175
closes: #463348
closes: #475982

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          rkpreviewbox  -  description
 
3
                             -------------------
 
4
    begin                : Wed Jan 24 2007
 
5
    copyright            : (C) 2007 by Thomas Friedrichsmeier
 
6
    email                : tfry@users.sourceforge.net
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
#include "rkpreviewbox.h"
 
18
 
 
19
#include <qlayout.h>
 
20
#include <qlabel.h>
 
21
#include <qcheckbox.h>
 
22
#include <qtimer.h>
 
23
 
 
24
#include <klocale.h>
 
25
 
 
26
#include "../rkglobals.h"
 
27
#include "../rbackend/rinterface.h"
 
28
#include "../misc/xmlhelper.h"
 
29
#include "../debug.h"
 
30
 
 
31
RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_component, QWidget *parent_widget) : RKComponent (parent_component, parent_widget) {
 
32
        RK_TRACE (PLUGIN);
 
33
 
 
34
        preview_active = false;
 
35
        last_plot_done = true;
 
36
        new_plot_pending = false;
 
37
 
 
38
        // get xml-helper
 
39
        XMLHelper *xml = XMLHelper::getStaticHelper ();
 
40
 
 
41
        // create and add property
 
42
        addChild ("state", state = new RKComponentPropertyBool (this, true, preview_active, "active", "inactive"));
 
43
        connect (state, SIGNAL (valueChanged (RKComponentPropertyBase *)), this, SLOT (changedState (RKComponentPropertyBase *)));
 
44
 
 
45
        // create checkbox
 
46
        QVBoxLayout *vbox = new QVBoxLayout (this, RKGlobals::spacingHint ());
 
47
        toggle_preview_box = new QCheckBox (xml->getStringAttribute (element, "label", i18n ("Preview"), DL_INFO), this);
 
48
        vbox->addWidget (toggle_preview_box);
 
49
        toggle_preview_box->setChecked (preview_active);
 
50
        connect (toggle_preview_box, SIGNAL (stateChanged (int)), this, SLOT (changedState (int)));
 
51
 
 
52
        // status lable
 
53
        status_label = new QLabel (QString::null, this);
 
54
        vbox->addWidget (status_label);
 
55
 
 
56
        // find and connect to code property of the parent
 
57
        QString dummy;
 
58
        RKComponentBase *cp = parentComponent ()->lookupComponent ("code", &dummy);
 
59
        if (cp && dummy.isNull () && (cp->type () == PropertyCode)) {
 
60
                code_property = static_cast<RKComponentPropertyCode *> (cp);
 
61
                connect (code_property, SIGNAL (valueChanged (RKComponentPropertyBase *)), this, SLOT (changedCode (RKComponentPropertyBase *)));
 
62
        } else {
 
63
                RK_DO (qDebug ("Could not find code property in preview box (remainder: %s)", dummy.latin1()), PLUGIN, DL_WARNING);
 
64
                code_property = 0;
 
65
        }
 
66
 
 
67
        // initialize
 
68
        update_timer = new QTimer (this);
 
69
        connect (update_timer, SIGNAL (timeout ()), this, SLOT (tryPreviewNow ()));
 
70
        updating = false;
 
71
        changedState (0);
 
72
}
 
73
 
 
74
RKPreviewBox::~RKPreviewBox () {
 
75
        RK_TRACE (PLUGIN);
 
76
 
 
77
        killPreview ();
 
78
}
 
79
 
 
80
void RKPreviewBox::changedState (RKComponentPropertyBase *) {
 
81
        RK_TRACE (PLUGIN);
 
82
 
 
83
        if (updating) return;
 
84
        updating = true;
 
85
        toggle_preview_box->setChecked (state->boolValue ());
 
86
        updating = false;
 
87
 
 
88
        tryPreview ();
 
89
        changed ();
 
90
}
 
91
 
 
92
void RKPreviewBox::changedCode (RKComponentPropertyBase *) {
 
93
        RK_TRACE (PLUGIN);
 
94
 
 
95
        tryPreview ();
 
96
}
 
97
 
 
98
void RKPreviewBox::changedState (int) {
 
99
        RK_TRACE (PLUGIN);
 
100
 
 
101
        state->setBoolValue (toggle_preview_box->isChecked ());
 
102
}
 
103
 
 
104
void RKPreviewBox::tryPreview () {
 
105
        RK_TRACE (PLUGIN);
 
106
 
 
107
        if (isEnabled () && toggle_preview_box->isChecked ()) update_timer->start (10, true);
 
108
        else killPreview ();
 
109
 
 
110
        updateStatusLabel ();
 
111
}
 
112
 
 
113
void RKPreviewBox::tryPreviewNow () {
 
114
        RK_TRACE (PLUGIN);
 
115
 
 
116
        if (!code_property) return;
 
117
        if (!parentComponent ()->isSatisfied ()) {
 
118
                return;
 
119
        }
 
120
        if (!parentComponent ()->isReady ()) {
 
121
                tryPreview ();
 
122
                return;
 
123
        }
 
124
 
 
125
        if (!last_plot_done) {          // if the last plot is not done, yet, wait before starting the next.
 
126
                new_plot_pending = true;
 
127
                updateStatusLabel ();
 
128
                return;
 
129
        }
 
130
 
 
131
        preview_active = true;
 
132
        QString dummy;
 
133
        RKGlobals::rInterface ()->issueCommand (dummy.sprintf (".rk.startPreviewDevice (\"%p\")", this), RCommand::Plugin | RCommand::Sync);
 
134
        RKGlobals::rInterface ()->issueCommand ("local({\n" + code_property->preview () + "})\n", RCommand::Plugin | RCommand::Sync, QString::null, this);
 
135
 
 
136
        last_plot_done = false;
 
137
        new_plot_pending = false;
 
138
 
 
139
        updateStatusLabel ();
 
140
}
 
141
 
 
142
void RKPreviewBox::killPreview () {
 
143
        RK_TRACE (PLUGIN);
 
144
 
 
145
        if (!preview_active) return;
 
146
        preview_active = false;
 
147
        QString command;
 
148
        RKGlobals::rInterface ()->issueCommand (command.sprintf (".rk.killPreviewDevice (\"%p\")", this), RCommand::Plugin | RCommand::Sync);
 
149
 
 
150
        last_plot_done = true;
 
151
        new_plot_pending = false;
 
152
}
 
153
 
 
154
void RKPreviewBox::rCommandDone (RCommand *) {
 
155
        RK_TRACE (PLUGIN);
 
156
 
 
157
        last_plot_done = true;
 
158
        if (new_plot_pending) tryPreview ();
 
159
 
 
160
        updateStatusLabel ();
 
161
}
 
162
 
 
163
void RKPreviewBox::updateStatusLabel () {
 
164
        RK_TRACE (PLUGIN);
 
165
 
 
166
        if (!toggle_preview_box->isChecked ()) {
 
167
                status_label->setText (i18n ("Preview disabled"));
 
168
        } else {
 
169
                if (parentComponent ()->isSatisfied () && parentComponent ()->isReady ()) {
 
170
                        if (last_plot_done && (!new_plot_pending)) {
 
171
                                status_label->setText (i18n ("Preview up to date"));
 
172
                        } else {
 
173
                                status_label->setText (i18n ("Preview updating"));
 
174
                        }
 
175
                } else {
 
176
                        status_label->setText (i18n ("Preview not (yet) possible"));
 
177
                }
 
178
        }
 
179
}
 
180
 
 
181
#include "rkpreviewbox.moc"