File: Workflow.cpp

package info (click to toggle)
fotowall 0.9-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,280 kB
  • sloc: cpp: 23,275; makefile: 51; sh: 25
file content (394 lines) | stat: -rw-r--r-- 13,753 bytes parent folder | download | duplicates (3)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://www.enricoros.com/opensource/fotowall                      *
 *                                                                         *
 *   Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com>               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "Workflow.h"

#include "Shared/PlugGui/Container.h"
#include "Shared/BreadCrumbBar.h"
#include "Shared/ButtonsDialog.h"
#include "App.h"
#include "Canvas/Canvas.h"
#include "CanvasAppliance.h"
#include "FotowallFile.h"
#include "HelpAppliance.h"
#include "HomeAppliance.h"
#include "Settings.h"
#ifndef NO_WORDCLOUD_APPLIANCE
#include "WordcloudAppliance.h"
#endif

#include <QTimer>


Workflow::Workflow(PlugGui::Container * container, BreadCrumbBar * bar, QObject * parent)
  : QObject(parent)
  , m_container(container)
  , m_bar(bar)
  , m_commandTimer(0)
  , m_processingQueue(false)
{
    // set the global reference
    App::workflow = this;

    // wire up the stacker and the bar
    connect(m_bar, SIGNAL(nodeClicked(quint32)), this, SLOT(slotNodeClicked(quint32)));

    // load a fotowall file if asked from the command line
    QStringList contentUrls = App::settings->commandlineUrls();
    if (contentUrls.size() == 1 && App::isFotowallFile(contentUrls.first())) {
        if (loadCanvas_A(contentUrls.first()))
            return;
    }

    // load content in a canvas
    if (!contentUrls.isEmpty()) {
        Canvas * canvas = new Canvas(m_container->physicalDpiX(), m_container->physicalDpiY(), this);
        canvas->resize(m_container->sceneViewSize());
        canvas->addAutoContent(contentUrls);
        pushNode(new CanvasAppliance(canvas));
        return;
    }

    // show the home screen (if no valid commandline args)
    pushNode(new HomeAppliance);
}

Workflow::~Workflow()
{
    // warn if we have any appliance left, shouldn't be
    while (!m_stack.isEmpty()) {
        qWarning("Workflow::~Workflow: not empty stack. dropping all");
        popNode(false);
    }

    // unset the global reference
    App::workflow = 0;
    m_container = 0;

    // this is an example of 'autosave-like function'
    //QString tempPath = QDir::tempPath() + QDir::separator() + "autosave.fotowall";
    //m_canvas->saveToFile(tempPath);

    // bar and container are external: don't delete
}

bool Workflow::loadCanvas_A(const QString & __fwFilePath)
{
    // ask for file name, if not provided - can CANCEL
    QString fwFilePath = __fwFilePath.isEmpty() ? FotowallFile::getLoadFotowallFile() : __fwFilePath;
    if (fwFilePath.isEmpty())
        return false;

    // schedule canvas loading
    scheduleCommand(Command::ResetToLevel);
    scheduleCommand(Command(Command::MasterCanvas, fwFilePath));
    return true;
}

void Workflow::startCanvas_A()
{
    // schedule canvas creation
    scheduleCommand(Command::ResetToLevel);
    scheduleCommand(Command::MasterCanvas);
}

void Workflow::stackSlaveCanvas_A(SingleResourceLoaner * resource)
{
    // schedule slave canvas
    scheduleCommand(Command(Command::SlaveCanvas, QVariant(), resource));
}

#ifndef NO_WORDCLOUD_APPLIANCE
void Workflow::startWordcloud_A()
{
    // schedule wordcloud creation
    scheduleCommand(Command::ResetToLevel);
    scheduleCommand(Command::MasterWordcloud);
}

void Workflow::stackSlaveWordcloud_A(SingleResourceLoaner * resource)
{
    // schedule slave wordcloud
    scheduleCommand(Command(Command::SlaveWordcloud, QVariant(), resource));
}
#endif

void Workflow::stackHelpAppliance()
{
    // if no help app, immediately stack it
    if (m_stack.isEmpty() || !dynamic_cast<HelpAppliance *>(m_stack.last().appliance))
        pushNode(new HelpAppliance);
}

void Workflow::popCurrentAppliance()
{
    if (!m_stack.isEmpty())
        scheduleCommand(Command(Command::ResetToLevel, m_stack.size() - 1));
}

QString Workflow::applianceName() const
{
    if (!m_stack.isEmpty())
        return m_stack.last().appliance->applianceName();
    return "Workflow";
}

bool Workflow::applianceCommand(int command)
{
    if (!m_stack.isEmpty())
        m_stack.last().appliance->applianceCommand(command);
    return false;
}

bool Workflow::requestExit()
{
    // save only if the user wants so
    bool allowSaving = false;

    // don't show the dialog from the home screen
    if (m_stack.size() > 1) {

        // count master appliances that can be saved
        int requiringSave = 0;
        foreach (const Node & node, m_stack)
            if (!node.loaner && node.appliance->appliancePendingChanges())
                ++requiringSave;

        // build the closure dialog
        ButtonsDialog quitAsk("Workflow-Exit", tr("Closing Fotowall..."));
        quitAsk.setMinimumWidth(350);
        quitAsk.setButtonText(QDialogButtonBox::Cancel, tr("Cancel"));
        if (requiringSave) {
            quitAsk.setMessage(tr("Are you sure you want to quit and lose your changes?"));
            quitAsk.setButtonText(QDialogButtonBox::Save, tr("Save"));
            quitAsk.setButtonText(QDialogButtonBox::Close, tr("Don't Save"));
            quitAsk.setButtons(QDialogButtonBox::Save | QDialogButtonBox::Close | QDialogButtonBox::Cancel);
        } else {
            quitAsk.setMessage(tr("Are you sure you want to quit?"));
            quitAsk.setButtonText(QDialogButtonBox::Close, tr("Quit"));
            quitAsk.setButtons(QDialogButtonBox::Close | QDialogButtonBox::Cancel);
        }

        // react to the dialog's answer
        QDialogButtonBox::StandardButton button = quitAsk.execute();
        if (button == QDialogButtonBox::Cancel)
            return false;
        if (button == QDialogButtonBox::Save)
            allowSaving = true;
    }

    // close (and maybe save) appliances
    while (!m_stack.isEmpty()) {
        // can cancel even here
        if (!popNode(allowSaving))
            return false;
    }
    return true;
}

void Workflow::scheduleCommand(const Command &command)
{
    if (!m_commandTimer) {
        m_commandTimer = new QTimer(this);
        m_commandTimer->setSingleShot(true);
        connect(m_commandTimer, SIGNAL(timeout()), this, SLOT(slotProcessQueue()));
    }
    m_commands.append(command);
    m_commandTimer->start(0);
}

bool Workflow::processCommand(const Workflow::Command & command)
{
    switch (command.type) {
        case Command::ResetToLevel: {
            int level = qMax(1, command.param.toInt());
            while (!dynamic_cast<HomeAppliance *>(m_stack.last().appliance) && m_stack.size() > level) {
                // ResetToLevel: save changes on each popped level
                bool allowSaving = false;
                const Node & node = m_stack.last();
                if (!node.loaner && node.appliance->appliancePendingChanges()) {
                    // build the closure dialog
                    ButtonsDialog saveDlg("Workflow-ResetToLevel", tr("Closing File"));
                    saveDlg.setMinimumWidth(350);
                    saveDlg.setButtonText(QDialogButtonBox::Cancel, tr("Cancel"));
                    saveDlg.setMessage(tr("Do you want to save your changes?"));
                    saveDlg.setButtonText(QDialogButtonBox::Save, tr("Save"));
                    saveDlg.setButtonText(QDialogButtonBox::Close, tr("Don't Save"));
                    saveDlg.setButtons(QDialogButtonBox::Save | QDialogButtonBox::Close | QDialogButtonBox::Cancel);

                    // react to the dialog's answer
                    QDialogButtonBox::StandardButton button = saveDlg.execute();
                    if (button == QDialogButtonBox::Cancel)
                        return false;
                    if (button == QDialogButtonBox::Save)
                        allowSaving = true;
                }

                // pop current node and break if asked for saving and canceled saving
                if (!popNode(allowSaving))
                    return false;
            }
            }return true;

        case Command::MasterCanvas: {
            Canvas * canvas = new Canvas(m_container->physicalDpiX(), m_container->physicalDpiY(), this);

            // load a file, if in params
            if (command.param.type() == QVariant::String) {
                QString fwFilePath = command.param.toString();
                if (!FotowallFile::read(fwFilePath, canvas, true))
                    qWarning("Workflow::processCommand: MasterCanvas: can't load canvas. Display error?");
            }

            // create the canvas appliance
            CanvasAppliance * canvasApp = new CanvasAppliance(canvas);
            pushNode(canvasApp);
            } return true;

        case Command::SlaveCanvas: {
            // get the Canvas out of the resource
            Canvas * canvas = static_cast<Canvas *>(qVariantValue<void *>(command.loaner->takeResource()));

            // create the canvas appliance
            if (canvas) {
                CanvasAppliance * canvasApp = new CanvasAppliance(canvas);
                pushNode(Node(canvasApp, command.loaner));
            }
            } return true;

#ifndef NO_WORDCLOUD_APPLIANCE
        case Command::MasterWordcloud: {
            Wordcloud::Cloud * cloud = new Wordcloud::Cloud(this);

            // TODO: load from file
            if (command.param.type() == QVariant::String)
                HERE;

            // create the wordcloud appliance
            WordcloudAppliance * wcApp = new WordcloudAppliance(cloud, this);
            pushNode(wcApp);
            } return true;

        case Command::SlaveWordcloud: {
            // get the Cloud out of the resource
            Wordcloud::Cloud * cloud = static_cast<Wordcloud::Cloud *>(qVariantValue<void *>(command.loaner->takeResource()));

            // create the wordcloud appliance
            WordcloudAppliance * wcApp = new WordcloudAppliance(cloud, this);
            pushNode(Node(wcApp, command.loaner));
            } return true;
#endif
    }

    // catch errors
    return false;
}

void Workflow::pushNode(const Node & node)
{
    // remove previous Appliance from container
    if (!m_stack.isEmpty() && m_container)
        m_stack.last().appliance->removeFromApplianceContainer();
    m_stack.append(node);
    // show this Appliance on container
    if (m_container)
        node.appliance->addToApplianceContainer(m_container);
    updateBreadcrumb();
}

bool Workflow::popNode(bool allowSave)
{
    // remove last Node and close its Appliance [can be canceled]
    if (!m_stack.isEmpty()) {

        // handle saving (Master & Changes & !disabled)
        const Node & node = m_stack.last();
        if (allowSave && !node.loaner && node.appliance->appliancePendingChanges())
            if (!node.appliance->applianceSave())
                return false;

        // specific resource return/deletion
        if (CanvasAppliance * cApp = dynamic_cast<CanvasAppliance *>(node.appliance)) {
            Canvas * canvas = cApp->takeCanvas();
            if (node.loaner)
                node.loaner->returnResource(qVariantFromValue((void *)canvas));
            else
                delete canvas;
#ifndef NO_WORDCLOUD_APPLIANCE
        } else if (WordcloudAppliance * wApp = dynamic_cast<WordcloudAppliance *>(node.appliance)) {
            Wordcloud::Cloud * cloud = wApp->takeCloud();
            if (node.loaner)
                node.loaner->returnResource(qVariantFromValue((void *)cloud));
            else
                delete cloud;
#endif
        } else if (!dynamic_cast<HomeAppliance *>(node.appliance) &&
                   !dynamic_cast<HelpAppliance *>(node.appliance))
            qWarning("Workflow::popNode: pop of appliance '%s' not handled", qPrintable(node.appliance->applianceName()));

        // now that we popped, delete the Appliance
        if (m_container)
            node.appliance->removeFromApplianceContainer();
        delete node.appliance;
        m_stack.removeLast();
    }

    // show the last-1
    if (m_container && !m_stack.isEmpty())
        m_stack.last().appliance->addToApplianceContainer(m_container);

    updateBreadcrumb();

    // tell that we popped
    return true;
}

void Workflow::updateBreadcrumb()
{
    // build the new breadcrumbbar's contents
    m_bar->clearNodes();
    if (!m_stack.isEmpty()) {
        quint32 index = 0;
        foreach (const Node & node, m_stack) {
            m_bar->addNode(index + 1, node.appliance->applianceName(), index);
            index++;
        }
    }

    // issue a complete repaint
    m_container->update();
}


void Workflow::slotNodeClicked(quint32 level)
{
    scheduleCommand(Command(Command::ResetToLevel, level));
}

void Workflow::slotProcessQueue()
{
    if (m_processingQueue) {
        qWarning("Workflow::slotProcessQueue: nesting detected -FIXME-");
        return;
    }
    m_processingQueue = true;
    while (!m_commands.isEmpty()) {
        // process the topmost command...
        if (!processCommand(m_commands.takeFirst())) {
            // ...and drop queue if it fails
            m_commands.clear();
        }
    }
    m_processingQueue = false;
}