source: src-qt4/PCDM/src/themeStruct.cpp @ d9b0b9e8

9.2-releasereleng/10.0releng/10.0.1
Last change on this file since d9b0b9e8 was d9b0b9e8, checked in by Ken Moore <ken@…>, 10 months ago

Add in a "simple" option for the PCDM desktop switcher.
Change the DESKTOP_ORIENTATION value to "simple" to use a small QComboBox on the toolbar for selecting a desktop environment instead of using the full selector

  • Property mode set to 100644
File size: 8.5 KB
Line 
1/* PCDM Login Manager:
2*  Written by Ken Moore (ken@pcbsd.org) 2012/2013
3*  Copyright(c) 2013 by the PC-BSD Project
4*  Available under the 3-clause BSD license
5*/
6
7#include "themeStruct.h"
8
9
10void ThemeStruct::loadThemeFile(QString filePath){
11  qDebug() << "Loading PCDM Theme File:" << filePath;
12  //Create the required Items:
13  itemNames.clear();
14  itemNames << "background" << "header" << "user" << "password" << "login" << "desktop" << "system" << "locale" << "keyboard" << "vkeyboard" << "toolbar" << "nextde" << "previousde";
15  items.clear();
16  for(int i=0; i<itemNames.length(); i++){ 
17    ThemeItem it; 
18    items << it; 
19  }
20  //Get the directory the current theme file is in
21  int li = filePath.lastIndexOf("/");
22  QString currDir = filePath;
23  if(li > 0){ currDir.truncate(li); }
24  else{ currDir.clear(); }
25  if(!currDir.endsWith("/")){ currDir.append("/"); }
26  //Now initialize variables for the file search
27  QString iconFileDir;
28  int numspacers = 0;
29 
30  QFile file(filePath);
31  if( !file.open(QIODevice::ReadOnly | QIODevice::Text)){ return; }
32  QTextStream in(&file);
33  while( !in.atEnd() ){
34    QString line = in.readLine().simplified();
35    if( line.isEmpty() || line.startsWith("#") ){}//ignore empty and comment lines
36    else if(line.startsWith("APP_STYLESHEET_START")){ 
37      //save all the following lines as the stylesheet
38      QStringList styleLines;
39      while( !line.startsWith("APP_STYLESHEET_END") && !in.atEnd() ){
40        line = in.readLine().simplified();
41        styleLines << line;
42      }
43      //Now compress into a single string and save it
44      applicationStyleSheet = styleLines.join(" ");
45    }else{
46      //Read off individual values for each item
47           
48      //Remove comments at the end of lines
49      line = line.section("#",0,0).simplified();
50      //Get the important bits from the line
51      QString prop = line.section("=",0,0).simplified(); //property
52      QString val = line.section("=",1,1).simplified();  //value
53      QString itemName = prop.section("_",0,0).toLower(); //item name
54      QString itemCat = prop.section("_",1,3).simplified();  //item category for value
55      int index = itemNames.indexOf( itemName ); //index for this item
56      //qDebug() << "File Item:" << prop << val << itemName << itemCat << index;
57      //Now save this item as appropriate
58      if(prop == "IMAGE_DIR" && !val.isEmpty()){ //default directory for icons
59        if(!val.endsWith("/")){val.append("/");}
60        iconFileDir = val; 
61        if(iconFileDir.startsWith("./")){ iconFileDir.replace(0,2,currDir); }
62      }else if( prop == "ADDSPACER" ){ //create a spacer item
63        itemNames << "spacer"+QString::number(numspacers);
64        numspacers++;
65        ThemeItem it;
66        items << it;
67        index = items.length() -1; //last item in the list
68        //read whether it is vertical
69        items[index].isVertical = (val.section("::",0,0).toLower() == "vertical");
70        //Now get the location
71        val = val.section("::",1,1).remove("[").remove("]").simplified();
72        //qDebug() << "ADDSPACER:" << line << val;
73        items[index].x1 = val.section(",",0,0).section("-",0,0).toInt();
74        items[index].x2 = -1;
75        items[index].y1 = val.section(",",1,1).section("-",0,0).toInt();
76        items[index].y2 = -1;
77      }else if( (index != -1) && !val.isEmpty() ){
78        //Save the value to the structure based upon the type of value it is
79        if(itemCat == "IMAGE"){
80            if(val.startsWith("/")){ items[index].icon = val; }
81            else{ items[index].icon = iconFileDir + val; }
82           
83        }else if(itemCat == "IMAGE_SIZE"){
84            items[index].iconSize = QSize( val.section("x",0,0).toInt(), val.section("x",1,1).toInt() );
85               
86        }else if(prop == "TOOLBAR_LOCATION"){
87            //special instructions for this item location
88            items[index].value = val; //should be [bottom | top | left | right]
89        }else if(prop == "TOOLBAR_STYLE"){
90            //special instructions for this item
91            items[index].icon = val; //should be [icononly | textonly | textbesideicon | textundericon ]
92        }else if(itemCat == "LOCATION"){
93            val.remove("[").remove("]").simplified(); //remove the brackets
94            QString x = val.section(",",0,0);
95            QString y = val.section(",",1,1);
96            if(x.indexOf("-") == -1){ items[index].x1 = x.toInt(); items[index].x2 = -1;}
97            else{ items[index].x1 = x.section("-",0,0).toInt(); items[index].x2 = x.section("-",1,1).toInt(); }
98            if(y.indexOf("-") == -1){ items[index].y1 = y.toInt(); items[index].y2 = -1;}
99            else{ items[index].y1 = y.section("-",0,0).toInt(); items[index].y2 = y.section("-",1,1).toInt(); }
100        }else if(itemCat == "ORIENTATION"){
101            items[index].isVertical = (val.toLower() == "vertical"); 
102            if(val.toLower()=="simple"){ items[index].value = "simple"; }
103        }else if(itemCat == "DISABLE"){
104            items[index].enabled = (val.toLower() != "true");
105        }
106      }
107    } // end if line is not a comment or empty
108  } // end reading lines from file
109  file.close();
110}
111
112ThemeItem ThemeStruct::exportItem(QString item){
113  int index = itemNames.indexOf(item);
114  ThemeItem TI;
115  if( index == -1 ){ 
116    qDebug() << "ThemeStruct: Invalid export item:"<<item;
117  }else{
118    //Now format the output
119    TI = items[index];
120  }
121  return TI;
122}
123
124void ThemeStruct::importItem(QString item, ThemeItem TI){
125  int index = itemNames.indexOf(item);
126  if( index == -1 ){ 
127    qDebug() << "ThemeStruct: Invalid import item:"<<item;
128  }else{
129    items[index] = TI;
130  }
131}
132
133bool ThemeStruct::validItem(QString item){
134  int index = itemNames.indexOf(item);
135  bool ret = FALSE;
136  if( index == -1 ){ 
137    qDebug() << "ThemeStruct: Invalid item:"<<item;
138  }else{
139    bool chk = items[index].enabled && items[index].value.isEmpty() && items[index].icon.isEmpty() && ( (items[index].x1==-1) || (items[index].y1==-1) );
140    ret = !chk; //true if the checks do not catch an empty item
141  }
142  return ret;
143}
144
145QStringList ThemeStruct::invalidItems(){
146  //Scan all the current Items and make a list of all the invalid ones
147   // This makes it easy to auto-populate these invalid items with defaults
148  QStringList inv;
149  for(int i=0; i<itemNames.length(); i++){
150    if( !validItem(itemNames[i]) ){ inv << itemNames[i]; }
151  }
152  return inv;
153}
154
155bool ThemeStruct::itemIsEnabled(QString item){
156  int index = itemNames.indexOf(item);
157  bool ret = FALSE;
158  if( index == -1 ){ 
159    qDebug() << "ThemeStruct: Invalid item:"<<item;
160  }else{
161    ret = items[index].enabled;
162  }
163  return ret;
164}
165
166bool ThemeStruct::itemIsVertical(QString item){
167  int index = itemNames.indexOf(item);
168  bool ret = FALSE;
169  if( index == -1 ){ 
170    qDebug() << "ThemeStruct: Invalid item:"<<item;
171  }else{
172    ret = items[index].isVertical; 
173  }
174  return ret;
175}
176
177QString ThemeStruct::itemValue(QString item){
178  int index = itemNames.indexOf(item);
179  QString ret;
180  if( index == -1 ){ 
181    qDebug() << "ThemeStruct: Invalid item:"<<item;
182    ret = "";
183  }else{
184    ret = items[index].value;
185  }
186  return ret;
187}
188
189QString ThemeStruct::itemIcon(QString item){
190  int index = itemNames.indexOf(item);
191  QString ret;
192  if( index == -1 ){ 
193    qDebug() << "ThemeStruct: Invalid item:"<<item;
194    ret = "";
195  }else{
196    ret = items[index].icon;
197  } 
198  //qDebug() << "Theme:" << item << "Icon:" << ret;
199  return ret;
200}
201
202QSize ThemeStruct::itemIconSize(QString item){
203  int index = itemNames.indexOf(item);
204  QSize ret;
205  if( index == -1 ){ 
206    qDebug() << "ThemeStruct: Invalid item:"<<item;
207  }else{
208    ret = items[index].iconSize;
209  }
210  //qDebug() << "Theme:" << item << "Size:" << ret;
211  return ret;
212}
213
214int ThemeStruct::itemLocation(QString item, QString variable){
215  int index = itemNames.indexOf(item);
216  int ret = 0;
217  if( index == -1 ){ 
218    qDebug() << "ThemeStruct: Invalid item:"<<item;
219    return ret;
220  }else{
221    variable = variable.toLower();
222    if(variable == "row"){ ret = items[index].x1; }
223    else if(variable == "col"){ ret = items[index].y1; }
224    else if(variable == "rowspan"){ 
225      int span = items[index].x2 - items[index].x1;
226      if(span < 0){ span = 0; } //Make sure it is always positive
227      ret = span +1; //add 1 for the end point
228    }else if(variable == "colspan"){
229      int span = items[index].y2 - items[index].y1;
230      if(span < 0){ span = 0; } //Make sure it is always positive
231      ret = span +1; //add 1 for the end point
232    }
233  }
234  //qDebug() << "Theme:" << item << variable << ret;
235  return ret; //catch for errors;
236}
237
238QString ThemeStruct::styleSheet(){
239  return applicationStyleSheet;
240}
241
242QStringList ThemeStruct::getSpacers(){
243  QStringList spc;
244  QString isvert;
245  for(int i=0; i<itemNames.length(); i++){
246    if( itemNames[i].startsWith("spacer") ){
247      if(items[i].isVertical){ isvert="true"; }
248      else{ isvert="false"; }
249      spc << isvert + "::" + QString::number(items[i].x1) +"::"+ QString::number(items[i].y1);
250    }
251  }
252  //qDebug() << "Spacers:" << spc;
253  return spc;
254}
Note: See TracBrowser for help on using the repository browser.