]> git.sven.stormbind.net Git - sven/vym.git/blob - src/flag.cpp
Replace Pierre as the maintainer
[sven/vym.git] / src / flag.cpp
1 #include "flag.h"
2
3 #include "file.h"
4
5 #include <QDebug>
6
7 /////////////////////////////////////////////////////////////////
8 // Flag
9 /////////////////////////////////////////////////////////////////
10 Flag::Flag()
11 {
12     // qDebug() << "Const Flag ()";
13     init();
14 }
15
16 Flag::Flag(const QString &fname)
17 {
18     // qDebug() << "Const Flag (fname)" << fname;
19     init();
20     if (!load(fname))
21         qWarning() << "Flag::Flag  Failed to load " << fname;
22 }
23
24 Flag::~Flag()
25 {
26     // qDebug() << "Destr Flag  this="<<this <<"  " << qPrintable(name) << "
27     // image=" << image;
28     if (image)
29         delete image;
30 }
31
32 void Flag::init()
33 {
34     action = NULL;
35     name = "undefined";
36     visible = true;
37     unsetGroup();
38
39     image = NULL;
40
41     state = false;
42     used = false;
43     type = UndefinedFlag;
44
45     uuid = QUuid::createUuid();
46 }
47
48 bool Flag::load(const QString &fn)
49 {
50     if (!image)
51         image = new ImageObj();
52
53     if (!image->load(fn))
54         return false;
55
56     if (fn.contains("svg")) {
57         image->setWidth(32); // FIXME-3 scale svg of flags
58     }
59
60     path = fn;
61
62     return true;
63 }
64
65 void Flag::setName(const QString &n)
66 {
67     name = n;
68     if (name.contains("/"))
69         name = basename(name);
70
71     name = name.section('.', 0, 0);
72 }
73
74 const QString Flag::getName() { return name; }
75
76 const QString Flag::getPath() { return path; }
77
78 void Flag::setVisible(bool b) { visible = b; }
79
80 bool Flag::isVisible() { return visible; }
81
82 void Flag::setGroup(const QString &n) { group = n; }
83
84 const QString Flag::getGroup() { return group; }
85
86 void Flag::unsetGroup() { group.clear(); }
87
88 void Flag::setToolTip(const QString &n) { tooltip = n; }
89
90 const QString Flag::getToolTip() { return tooltip; }
91
92 ImageObj *Flag::getImageObj()
93 {
94     if (image)
95         return image;
96     else
97         return NULL;
98 }
99
100 void Flag::setAction(QAction *a) { action = a; }
101
102 QAction *Flag::getAction() { return action; }
103
104 void Flag::setUsed(bool b) { used = b; }
105
106 bool Flag::isUsed() { return used; }
107
108 Flag::FlagType Flag::getType() { return type; }
109
110 void Flag::setType(Flag::FlagType t) { type = t; }
111
112 void Flag::setUuid(const QUuid &id) { uuid = id; }
113
114 QUuid Flag::getUuid() { return uuid; }
115
116 QString Flag::getDefinition(const QString &prefix)
117 {
118     if (type == Flag::UserFlag) {
119         QString url = "flags/" + prefix + uuid.toString() + "-" + name +
120                       image->getExtension();
121         QStringList attributes;
122         attributes << attribut("name", name);
123         attributes << attribut("href", QString("file:%1").arg(url));
124         attributes << attribut("uuid", uuid.toString());
125         return singleElement("userflagdef", attributes);
126     }
127     else
128         return QString();
129 }
130
131 void Flag::saveDataToDir(const QString &dirPath)
132 {
133     if (image) {
134         path = dirPath + "/" + uuid.toString() + "-" + name +
135                image->getExtension();
136         image->save(path);
137     }
138 }
139
140 QString Flag::saveState()
141 {
142     if (type == Flag::UserFlag)
143         return singleElement("userflag", attribut("name", name) +
144                                              attribut("uuid", uuid.toString()));
145     else
146         return valueElement("standardflag", name);
147 }