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
|
// SPDX-FileCopyrightText: 2003 Dominique Devriese <devriese@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "text_type.h"
#include "bogus_imp.h"
#include "line_imp.h"
#include "object_drawer.h"
#include "point_imp.h"
#include "text_imp.h"
#include "../kig/kig_commands.h"
#include "../kig/kig_part.h"
#include "../kig/kig_view.h"
#include "../misc/coordinate_system.h"
#include "../modes/label.h"
#include <algorithm>
#include <cmath>
#include <iterator>
#include <QApplication>
#include <QClipboard>
#include <QFontDialog>
#include <QStringList>
static const ArgsParser::spec arggspeccs[] = {{IntImp::stype(), "UNUSED", {}, false},
{PointImp::stype(), "UNUSED", {}, false},
{StringImp::stype(), "UNUSED", {}, false}};
// KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( GenericTextType )
GenericTextType::GenericTextType(const char fulltypename[])
: ObjectType(fulltypename)
, mparser(arggspeccs, 3)
{
}
GenericTextType::~GenericTextType()
{
}
const ObjectImpType *GenericTextType::resultId() const
{
return TextImp::stype();
}
const ObjectImpType *GenericTextType::impRequirement(const ObjectImp *o, const Args &args) const
{
assert(args.size() >= 3);
Args firstthree(args.begin(), args.begin() + 3);
if (o == args[0] || o == args[1] || o == args[2])
return argParser().impRequirement(o, firstthree);
else
return ObjectImp::stype();
}
ObjectImp *GenericTextType::calc(const Args &parents, const KigDocument &doc) const
{
if (parents.size() < 3)
return new InvalidImp;
Args firstthree(parents.begin(), parents.begin() + 3);
Args varargs(parents.begin() + 3, parents.end());
if (!mparser.checkArgs(firstthree))
return new InvalidImp;
int frame = static_cast<const IntImp *>(firstthree[0])->data();
bool needframe = frame != 0;
const Coordinate t = static_cast<const PointImp *>(firstthree[1])->coordinate();
QString s = static_cast<const StringImp *>(firstthree[2])->data();
for (Args::iterator i = varargs.begin(); i != varargs.end(); ++i)
(*i)->fillInNextEscape(s, doc);
if (varargs.size() == 1 && varargs[0]->inherits(DoubleImp::stype())) {
double value = static_cast<const DoubleImp *>(varargs[0])->data();
return new NumericTextImp(s, t, needframe, value);
} else if (varargs.size() == 1 && varargs[0]->inherits(TestResultImp::stype())) {
bool value = static_cast<const TestResultImp *>(varargs[0])->truth();
return new BoolTextImp(s, t, needframe, value);
} else {
return new TextImp(s, t, needframe);
}
}
bool GenericTextType::canMove(const ObjectTypeCalcer &) const
{
return true;
}
bool GenericTextType::isFreelyTranslatable(const ObjectTypeCalcer &) const
{
return true;
}
void GenericTextType::move(ObjectTypeCalcer &ourobj, const Coordinate &to, const KigDocument &d) const
{
const std::vector<ObjectCalcer *> parents = ourobj.parents();
assert(parents.size() >= 3);
const std::vector<ObjectCalcer *> firstthree(parents.begin(), parents.begin() + 3);
if (dynamic_cast<ObjectConstCalcer *>(firstthree[1])) {
ObjectConstCalcer *c = static_cast<ObjectConstCalcer *>(firstthree[1]);
c->setImp(new PointImp(to));
} else
firstthree[1]->move(to, d);
}
const ArgsParser &GenericTextType::argParser() const
{
return mparser;
}
const Coordinate GenericTextType::moveReferencePoint(const ObjectTypeCalcer &ourobj) const
{
assert(ourobj.imp()->inherits(TextImp::stype()));
return static_cast<const TextImp *>(ourobj.imp())->coordinate();
}
std::vector<ObjectCalcer *> GenericTextType::sortArgs(const std::vector<ObjectCalcer *> &os) const
{
assert(os.size() >= 3);
std::vector<ObjectCalcer *> ret(os.begin(), os.begin() + 3);
ret = mparser.parse(ret);
std::copy(os.begin() + 3, os.end(), std::back_inserter(ret));
return ret;
}
Args GenericTextType::sortArgs(const Args &args) const
{
assert(args.size() >= 3);
Args ret(args.begin(), args.begin() + 3);
ret = mparser.parse(ret);
std::copy(args.begin() + 3, args.end(), std::back_inserter(ret));
return ret;
}
std::vector<ObjectCalcer *> GenericTextType::movableParents(const ObjectTypeCalcer &ourobj) const
{
const std::vector<ObjectCalcer *> parents = ourobj.parents();
assert(parents.size() >= 3);
std::vector<ObjectCalcer *> ret = parents[1]->movableParents();
ret.push_back(parents[1]);
return ret;
}
bool GenericTextType::isDefinedOnOrThrough(const ObjectImp *, const Args &) const
{
return false;
}
QStringList GenericTextType::specialActions() const
{
QStringList ret;
ret << i18n("&Copy Text");
ret << i18n("&Toggle Frame");
ret << i18n("Set &Font...");
return ret;
}
void GenericTextType::executeAction(int i, ObjectHolder &oh, ObjectTypeCalcer &c, KigPart &doc, KigWidget &w, NormalMode &) const
{
std::vector<ObjectCalcer *> parents = c.parents();
assert(parents.size() >= 3);
std::vector<ObjectCalcer *> firstthree(parents.begin(), parents.begin() + 3);
assert(mparser.checkArgs(firstthree));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[0]));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[2]));
if (i == 0) {
QClipboard *cb = QApplication::clipboard();
// copy the text into the clipboard
const TextImp *ti = static_cast<const TextImp *>(c.imp());
cb->setText(ti->text(), QClipboard::Clipboard);
} else if (i == 1) {
// toggle label frame
int n = (static_cast<const IntImp *>(firstthree[0]->imp())->data() + 1) % 2;
KigCommand *kc = new KigCommand(doc, i18n("Toggle Label Frame"));
kc->addTask(new ChangeObjectConstCalcerTask(static_cast<ObjectConstCalcer *>(firstthree[0]), new IntImp(n)));
doc.history()->push(kc);
} else if (i == 2) {
// change label font
QFont f = oh.drawer()->font();
bool result;
f = QFontDialog::getFont(&result, f, &w);
if (!result)
return;
KigCommand *kc = new KigCommand(doc, i18n("Change Label Font"));
kc->addTask(new ChangeObjectDrawerTask(&oh, oh.drawer()->getCopyFont(f)));
doc.history()->push(kc);
} else
assert(false);
}
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(TextType)
TextType::TextType()
: GenericTextType("Label")
{
}
TextType::~TextType()
{
}
const TextType *TextType::instance()
{
static const TextType t;
return &t;
}
QStringList TextType::specialActions() const
{
QStringList ret = GenericTextType::specialActions();
ret << i18n("&Redefine...");
return ret;
}
void TextType::executeAction(int i, ObjectHolder &o, ObjectTypeCalcer &c, KigPart &doc, KigWidget &w, NormalMode &nm) const
{
std::vector<ObjectCalcer *> parents = c.parents();
assert(parents.size() >= 3);
std::vector<ObjectCalcer *> firstthree(parents.begin(), parents.begin() + 3);
assert(argParser().checkArgs(firstthree));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[0]));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[2]));
const int parentactions = GenericTextType::specialActions().count();
if (i < parentactions)
GenericTextType::executeAction(i, o, c, doc, w, nm);
else if (i == parentactions) {
assert(dynamic_cast<ObjectTypeCalcer *>(o.calcer()));
// redefine..
TextLabelRedefineMode m(doc, static_cast<ObjectTypeCalcer *>(o.calcer()));
doc.runMode(&m);
} else
assert(false);
}
KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE(NumericTextType)
NumericTextType::NumericTextType()
: GenericTextType("NumericLabel")
{
}
NumericTextType::~NumericTextType()
{
}
const NumericTextType *NumericTextType::instance()
{
static const NumericTextType t;
return &t;
}
QStringList NumericTextType::specialActions() const
{
QStringList ret = GenericTextType::specialActions();
ret << i18n("Change &Value...");
return ret;
}
void NumericTextType::executeAction(int i, ObjectHolder &o, ObjectTypeCalcer &c, KigPart &doc, KigWidget &w, NormalMode &nm) const
{
std::vector<ObjectCalcer *> parents = c.parents();
assert(parents.size() == 4);
std::vector<ObjectCalcer *> firstthree(parents.begin(), parents.begin() + 3);
assert(o.imp()->inherits(NumericTextImp::stype()));
assert(argParser().checkArgs(firstthree));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[0]));
assert(dynamic_cast<ObjectConstCalcer *>(firstthree[2]));
const int parentactions = GenericTextType::specialActions().count();
if (i < parentactions)
GenericTextType::executeAction(i, o, c, doc, w, nm);
else if (i == parentactions) {
bool ok;
ObjectConstCalcer *valuecalcer = dynamic_cast<ObjectConstCalcer *>(parents[3]);
assert(valuecalcer);
double oldvalue = static_cast<const NumericTextImp *>(o.imp())->getValue();
double value = getDoubleFromUser(i18n("Set Value"), i18n("Enter the new value:"), oldvalue, &w, &ok, -2147483647, 2147483647, 7);
if (!ok)
return;
MonitorDataObjects mon(parents);
valuecalcer->setImp(new DoubleImp(value));
KigCommand *kc = new KigCommand(doc, i18n("Change Displayed Value"));
mon.finish(kc);
doc.history()->push(kc);
} else
assert(false);
}
|