]> git.sven.stormbind.net Git - sven/vym.git/blob - flagrow.cpp
Import Upstream version 2.6.11
[sven/vym.git] / flagrow.cpp
1 #include <QDebug>
2
3 #include "flagrow.h"
4
5 extern bool debug;
6
7 /////////////////////////////////////////////////////////////////
8 // FlagRow
9 /////////////////////////////////////////////////////////////////
10 FlagRow::FlagRow()
11 {
12     toolBar=NULL;
13     masterRow=NULL;
14 //    qDebug()<< "Const FlagRow ()";
15 }
16
17 FlagRow::~FlagRow()
18 {
19     //qDebug()<< "Destr FlagRow";
20 }
21
22 void FlagRow::addFlag (Flag *flag)
23 {
24     Flag *f=new Flag;
25 ;
26     f->copy (flag);
27     flags.append (f);
28     activeNames.append (flag->getName());
29 }
30
31 Flag* FlagRow::getFlag (const QString &name)
32 {
33     int i=0;
34     while (i<=flags.size()-1)
35     {
36         if (flags.at(i)->getName()==name)
37             return flags.at(i);
38         i++;    
39     }
40     return NULL;
41 }
42
43 QStringList FlagRow::activeFlagNames()
44 {
45     return activeNames;
46 }
47
48
49 bool FlagRow::isActive (const QString &name)    
50 {
51     QString n;
52     foreach (n,activeNames)
53         if (n==name) return true;
54     return false;   
55 }
56
57 bool FlagRow::toggle (const QString &name, FlagRow *masterRow)
58 {
59     if (isActive(name) )
60         return deactivate (name);
61     else
62     {
63         if (!activate (name) ) return false;    
64
65         // Deactivate group
66         if (!masterRow) return false;
67
68         Flag *flag=masterRow->getFlag (name);
69         if (!flag) return false;
70         QString mygroup=flag->getGroup();
71
72         for (int i=0;i<activeNames.size();++i)
73         {
74             flag=masterRow->getFlag (activeNames.at(i) );
75             if (name!=activeNames.at(i) && !mygroup.isEmpty() && mygroup==flag->getGroup())
76                 deactivate (activeNames.at(i));
77         }
78         return true;
79     }
80 }
81
82 bool FlagRow::activate (const QString &name)
83 {
84     if (isActive (name)) 
85     {
86         if (debug) qWarning ()<<QString("FlagRow::activate - %1 is already active").arg(name);
87         return false;
88     }
89
90     if (!masterRow)
91     {
92         qWarning()<<"FlagRow::activate - no masterRow to activate "<<name;
93         return false;
94     }
95
96     // Check, if flag exists after all...
97     Flag *flag=masterRow->getFlag (name);
98     if (!flag)
99     {
100         qWarning()<<"FlagRow::activate - flag "<<name<<" does not exist here!";
101         return false;
102     }
103
104     activeNames.append (name);
105     return true;
106 }
107
108
109 bool FlagRow::deactivate (const QString &name)
110 {
111     int n=activeNames.indexOf (name);
112     if (n>=0)
113     {
114         activeNames.removeAt(n);
115         return true;
116     }
117     if (debug) 
118         qWarning ()<<QString("FlagRow::deactivate - %1 is not active").arg(name);
119     return false;
120 }
121
122 bool FlagRow::deactivateGroup (const QString &gname) 
123 {
124     if (!masterRow) return false;
125     if (gname.isEmpty()) return false;
126
127     foreach (QString s, activeNames )
128     {
129         Flag *flag=masterRow->getFlag (s);
130         if (flag && gname == flag->getGroup())
131             deactivate (s);
132     }
133     return true;
134 }
135
136 void FlagRow::deactivateAll ()
137 {
138     if (!toolBar) activeNames.clear();
139 }
140
141 void FlagRow::setEnabled (bool b)
142 {
143     toolBar->setEnabled (b);
144 }
145
146 void FlagRow::resetUsedCounter()
147 {
148     for (int i=0; i<flags.size(); ++i)
149         flags.at(i)->setUsed (false);
150 }
151
152 QString FlagRow::saveToDir (const QString &tmpdir,const QString &prefix, bool writeflags) 
153 {
154     // Build xml string
155     QString s;
156     
157     if (!toolBar)
158     {
159         if (!activeNames.isEmpty())
160         for (int i=0; i<activeNames.size(); ++i)
161         {
162             // save flag to xml, if flag is set 
163             s+=valueElement("standardflag",activeNames.at(i));
164
165             // and tell parentRow, that this flag is used   
166             masterRow->getFlag(activeNames.at(i))->setUsed(true);
167         }   
168     } else
169         // Save icons to dir, if verbose is set (xml export)
170         // and I am a master
171         // and this flag is really used somewhere
172         if (writeflags)
173             for (int i=0; i<flags.size(); ++i)
174                 if (flags.at(i)->isUsed()) flags.at(i)->saveToDir (tmpdir,prefix);
175     return s;       
176 }
177
178 void FlagRow::setName (const QString &n)
179 {
180     rowName=n;
181 }
182
183 void FlagRow::setToolBar (QToolBar *tb)
184 {
185     toolBar=tb;
186 }
187
188 void FlagRow::setMasterRow (FlagRow *row)
189 {
190     masterRow=row; 
191 }
192
193 void FlagRow::updateToolBar (const QStringList &activeNames)
194 {
195     if (toolBar )
196     {
197         for (int i=0;i<flags.size();++i)
198             flags.at(i)->getAction()->setChecked (false);
199         for (int i=0;i<flags.size();++i)
200         {
201             int n=activeNames.indexOf (flags.at(i)->getName());
202             if (n>=0)
203                 flags.at(i)->getAction()->setChecked (true);    
204         }
205     }
206 }
207
208