~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/ipelets/templates/templates.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// --------------------------------------------------------------------
 
2
// Ipelet for handling template objects
 
3
// --------------------------------------------------------------------
 
4
/*
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2004  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
10
    under the terms of the GNU General Public License as published by
 
11
    the Free Software Foundation; either version 2 of the License, or
 
12
    (at your option) any later version.
 
13
 
 
14
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
*/
 
30
 
 
31
#include "ipelib.h"
 
32
#include "ipestyle.h"
 
33
#include "iperef.h"
 
34
 
 
35
#include "dlguse.h"
 
36
#include <qstringlist.h>
 
37
#include <qcombobox.h>
 
38
 
 
39
inline QString QIpe(const IpeString &str)
 
40
{
 
41
  return QString::fromUtf8(str.CString());
 
42
}
 
43
 
 
44
// --------------------------------------------------------------------
 
45
 
 
46
class TemplateIpelet : public Ipelet {
 
47
public:
 
48
  virtual int IpelibVersion() const { return IPELIB_VERSION; }
 
49
  virtual int NumFunctions() const { return 5; }
 
50
  virtual const char *Label() const { return "Templates"; }
 
51
  virtual const char *SubLabel(int function) const;
 
52
  virtual void Run(int function, IpePage *page, IpeletHelper *helper);
 
53
private:
 
54
  void CreateTemplate(IpePage *page, IpeletHelper *helper, bool newSheet);
 
55
  void UseTemplate(IpePage *page, IpeletHelper *helper, bool clone);
 
56
  void InsertBackground(IpeletHelper *helper);
 
57
};
 
58
 
 
59
// --------------------------------------------------------------------
 
60
 
 
61
const char * const sublabel[] = {
 
62
  "Create template (in new style sheet)",
 
63
  "Create template (in top style sheet)",
 
64
  "Use template",
 
65
  "Clone template (for editing it)",
 
66
  "Insert background on all pages"
 
67
};
 
68
 
 
69
const char *TemplateIpelet::SubLabel(int function) const
 
70
{
 
71
  return sublabel[function];
 
72
}
 
73
 
 
74
// --------------------------------------------------------------------
 
75
 
 
76
void TemplateIpelet::CreateTemplate(IpePage *page, IpeletHelper *helper,
 
77
                                    bool newSheet)
 
78
{
 
79
  IpePage::iterator it = page->PrimarySelection();
 
80
  if (it == page->end()) {
 
81
    helper->Message("No object selected");
 
82
    return;
 
83
  }
 
84
 
 
85
  IpeDocument *doc = helper->EditDocument();
 
86
  if (!newSheet && doc->StyleSheet()->IsStandard()) {
 
87
    helper->MessageBox("I cannot add template to standard style sheet.",
 
88
                       "Dismiss", 0, 0);
 
89
    return;
 
90
  }
 
91
 
 
92
  IpeString name;
 
93
  if (!helper->GetString("Choose name for template:", name))
 
94
    return;
 
95
 
 
96
  if (newSheet) {
 
97
    IpeStyleSheet *ns = new IpeStyleSheet(doc->Repository());
 
98
    ns->SetCascade(doc->GetStyleSheet());
 
99
    doc->SetStyleSheet(ns);
 
100
  }
 
101
 
 
102
  IpeStyleSheet *sheet = doc->GetStyleSheet();
 
103
  IpeAttribute tpl =
 
104
    doc->Repository()->MakeSymbol(IpeAttribute::ETemplate, name);
 
105
  sheet->AddTemplate(tpl, it->Object()->Clone());
 
106
}
 
107
 
 
108
// --------------------------------------------------------------------
 
109
 
 
110
void TemplateIpelet::UseTemplate(IpePage *page, IpeletHelper *helper,
 
111
                                 bool clone)
 
112
{
 
113
  const IpeStyleSheet *sheet = helper->StyleSheet();
 
114
  IpeAttributeSeq seq;
 
115
  sheet->AllNames(IpeAttribute::ETemplate, seq);
 
116
  if (seq.empty()) {
 
117
    helper->MessageBox("Your document's style sheet contains no templates",
 
118
                       "Dismiss", 0, 0);
 
119
    return;
 
120
  }
 
121
  QStringList sl;
 
122
  for (IpeAttributeSeq::const_iterator it = seq.begin(); it != seq.end(); ++it)
 
123
    sl.append(QIpe(sheet->Repository()->String(*it)));
 
124
 
 
125
  DlgUseTemplate dlg(0, 0, true);
 
126
  dlg.iTemplate->insertStringList(sl);
 
127
  if (dlg.exec() == QDialog::Accepted) {
 
128
    IpeAttribute tpl = seq[dlg.iTemplate->currentItem()];
 
129
    IpeObject *obj = new IpeReference(sheet, tpl);
 
130
    if (clone) {
 
131
      const IpeObject *obj = sheet->FindTemplate(tpl);
 
132
      page->push_back(IpePgObject(IpePgObject::ESecondary,
 
133
                                  helper->CurrentLayer(), obj->Clone()));
 
134
      helper->Message("Inserted a copy of the template");
 
135
    } else {
 
136
      page->push_back(IpePgObject(IpePgObject::ESecondary,
 
137
                                  helper->CurrentLayer(), obj));
 
138
      helper->Message("Inserted reference to template");
 
139
    }
 
140
  }
 
141
}
 
142
 
 
143
// --------------------------------------------------------------------
 
144
 
 
145
void TemplateIpelet::InsertBackground(IpeletHelper *helper)
 
146
{
 
147
  IpeDocument *doc = helper->EditDocument();
 
148
  IpeAttribute bga =
 
149
    doc->Repository()->MakeSymbol(IpeAttribute::ETemplate, "background");
 
150
  if (!doc->StyleSheet()->FindTemplate(bga)) {
 
151
    helper->MessageBox("No template 'background' is defined in "
 
152
                       "the current style sheet",
 
153
                       "Dismiss", 0, 0);
 
154
    return;
 
155
  }
 
156
 
 
157
  if (helper->MessageBox("Do you wish to add a 'background' layer with "
 
158
                         "a reference to the 'background' template to "
 
159
                         "every page of your document?",
 
160
                         "Yes, please", "No, thank you", 0) != 0)
 
161
    return;
 
162
 
 
163
  for (IpeDocument::iterator it = doc->begin(); it != doc->end(); ++it) {
 
164
    IpePage *page = *it;
 
165
    if (page->FindLayer("background") >= 0)
 
166
      continue;
 
167
 
 
168
    IpeLayer bglayer("background");
 
169
    bglayer.SetLocked(true);
 
170
    page->AddLayer(bglayer, 0);
 
171
 
 
172
    IpeObject *obj = new IpeReference(doc->StyleSheet(), bga);
 
173
    page->push_back(IpePgObject(IpePgObject::ENone, 0, obj));
 
174
  }
 
175
  helper->Message("Background added to all pages");
 
176
}
 
177
 
 
178
// --------------------------------------------------------------------
 
179
 
 
180
void TemplateIpelet::Run(int function, IpePage *page, IpeletHelper *helper)
 
181
{
 
182
  switch (function) {
 
183
  case 0:
 
184
    CreateTemplate(page, helper, true);
 
185
    break;
 
186
  case 1:
 
187
    CreateTemplate(page, helper, false);
 
188
    break;
 
189
  case 2:
 
190
    UseTemplate(page, helper, false);
 
191
    break;
 
192
  case 3:
 
193
    UseTemplate(page, helper, true);
 
194
    break;
 
195
  case 4:
 
196
    InsertBackground(helper);
 
197
  default:
 
198
    break;
 
199
  }
 
200
}
 
201
 
 
202
// --------------------------------------------------------------------
 
203
 
 
204
IPELET_DECLARE Ipelet *NewIpelet()
 
205
{
 
206
  return new TemplateIpelet;
 
207
}
 
208
 
 
209
// --------------------------------------------------------------------