File: formdocument.h

package info (click to toggle)
sailcut 1.3.2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,504 kB
  • ctags: 1,066
  • sloc: cpp: 11,930; sh: 9,126; makefile: 214
file content (145 lines) | stat: -rw-r--r-- 3,425 bytes parent folder | download
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
/*
 * Copyright (C) 1993-2008 Robert & Jeremy Laine
 * See AUTHORS file for a full list of contributors.
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef FORMDOCUMENT_H
#define FORMDOCUMENT_H

#include <QWidget>
#include <vector>
#include "sailviewer-tabs.h"

using namespace std;

// forward definitions
class CPrefs;
class CException;
class QMenu;

#define T_KEYPRESS \
  void keyPressEvent(QKeyEvent * e) { \
    tabs->panel[tabs->currentIndex()]->keyPressEvent(e); \
  };

/** A dialog holding a Sailcut document.
 */
class CFormDocument : public QWidget
{
    Q_OBJECT

public:
    /** The constructor. */
    CFormDocument(CPrefs *myPrefs, QWidget *parent)
            : QWidget(parent), prefs(myPrefs)
    {};
    virtual bool open(const QString &newfile) = 0;
    virtual bool save() = 0;
    virtual bool saveAs() = 0;

public:
    /** Extra submenus for the File menu */
    vector<QMenu*> extraFileMenus;
    /** Extra actions for the View menu */
    vector<QAction*> extraViewActions;
    /** The current filename. */
    QString filename;

protected:
    /** The user preferences. */
    CPrefs *prefs;
    /** An optional tabbed widget */
    CSailViewerTabs *tabs;
};


template <class deftype, class writertype>
class CFormDocumentTmpl : public CFormDocument
{
public:
    /** The constructor.
     *
     * @params myPrefs
     */
    CFormDocumentTmpl(CPrefs *myPrefs, QWidget *parent)
            : CFormDocument(myPrefs, parent)
    {}
    ;

    /**
     * Reads the definition from an XML file.
     */
    virtual bool open(const QString &newfile)
    {
        try
        {
            deftype newdef = writertype().read(newfile);
            setDef(newdef);
            filename = newfile;
            return true;
        }
        catch (CException e)
        {
            writertype::readErrorMessage();
        }
        return false;
    };

    /**
     * Saves the definition to an XML file.
     */
    virtual bool save()
    {
        if ( filename.isEmpty() )
            return saveAs();

        // try writing to file, catch exception
        try
        {
            writertype().write(def, filename);
            return true;
        }
        catch (CException e)
        {
            writertype::writeErrorMessage();
        }
        return false;
    };

    /**
     * Opens a dialog to select the XML to which the definition should be saved.
     */
    virtual bool saveAs()
    {
        QString newfile = writertype().writeDialog(def, filename);

        if ( !newfile.isNull() )
        {
            filename = newfile;
            return true;
        }
        return false;
    };

protected:
    virtual void setDef(const deftype& newdef) = 0;

protected:
    deftype def;
};

#endif