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

9.1-release9.2-releasereleng/10.0releng/10.0.1
Last change on this file since 1620346 was 1620346, checked in by Kris Moore <kris@…>, 12 months ago

Initial import of PC-BSD /current/ SVN repo

  • 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        }else if(itemCat == "DISABLE"){
103            items[index].enabled = (val.toLower() != "true");
104        }
105      }
106    } // end if line is not a comment or empty
107  } // end reading lines from file
108  file.close();
109}
110
111ThemeItem ThemeStruct::exportItem(QString item){
112  int index = itemNames.indexOf(item);
113  ThemeItem TI;
114  if( index == -1 ){ 
115    qDebug() << "ThemeStruct: Invalid export item:"<<item;
116  }else{
117    //Now format the output
118    TI = items[index];
119  }
120  return TI;
121}
122
123void ThemeStruct::importItem(QString item, ThemeItem TI){
124  int index = itemNames.indexOf(item);
125  if( index == -1 ){ 
126    qDebug() << "ThemeStruct: Invalid import item:"<<item;
127  }else{
128    items[index] = TI;
129  }
130}
131
132bool ThemeStruct::validItem(QString item){
133  int index = itemNames.indexOf(item);
134  bool ret = FALSE;
135  if( index == -1 ){ 
136    qDebug() << "ThemeStruct: Invalid item:"<<item;
137  }else{
138    bool chk = items[index].enabled && items[index].value.isEmpty() && items[index].icon.isEmpty() && ( (items[index].x1==-1) || (items[index].y1==-1) );
139    ret = !chk; //true if the checks do not catch an empty item
140  }
141  return ret;
142}
143
144QStringList ThemeStruct::invalidItems(){
145  //Scan all the current Items and make a list of all the invalid ones
146   // This makes it easy to auto-populate these invalid items with defaults
147  QStringList inv;
148  for(int i=0; i<itemNames.length(); i++){
149    if( !validItem(itemNames[i]) ){ inv << itemNames[i]; }
150  }
151  return inv;
152}
153
154bool ThemeStruct::itemIsEnabled(QString item){
155  int index = itemNames.indexOf(item);
156  bool ret = FALSE;
157  if( index == -1 ){ 
158    qDebug() << "ThemeStruct: Invalid item:"<<item;
159  }else{
160    ret = items[index].enabled;
161  }
162  return ret;
163}
164
165bool ThemeStruct::itemIsVertical(QString item){
166  int index = itemNames.indexOf(item);
167  bool ret = FALSE;
168  if( index == -1 ){ 
169    qDebug() << "ThemeStruct: Invalid item:"<<item;
170  }else{
171    ret = items[index].isVertical; 
172  }
173  return ret;
174}
175
176QString ThemeStruct::itemValue(QString item){
177  int index = itemNames.indexOf(item);
178  QString ret;
179  if( index == -1 ){ 
180    qDebug() << "ThemeStruct: Invalid item:"<<item;
181    ret = "";
182  }else{
183    ret = items[index].value;
184  }
185  return ret;
186}
187
188QString ThemeStruct::itemIcon(QString item){
189  int index = itemNames.indexOf(item);
190  QString ret;
191  if( index == -1 ){ 
192    qDebug() << "ThemeStruct: Invalid item:"<<item;
193    ret = "";
194  }else{
195    ret = items[index].icon;
196  } 
197  //qDebug() << "Theme:" << item << "Icon:" << ret;
198  return ret;
199}
200
201QSize ThemeStruct::itemIconSize(QString item){
202  int index = itemNames.indexOf(item);
203  QSize ret;
204  if( index == -1 ){ 
205    qDebug() << "ThemeStruct: Invalid item:"<<item;
206  }else{
207    ret = items[index].iconSize;
208  }
209  //qDebug() << "Theme:" << item << "Size:" << ret;
210  return ret;
211}
212
213int ThemeStruct::itemLocation(QString item, QString variable){
214  int index = itemNames.indexOf(item);
215  int ret = 0;
216  if( index == -1 ){ 
217    qDebug() << "ThemeStruct: Invalid item:"<<item;
218    return ret;
219  }else{
220    variable = variable.toLower();
221    if(variable == "row"){ ret = items[index].x1; }
222    else if(variable == "col"){ ret = items[index].y1; }
223    else if(variable == "rowspan"){ 
224      int span = items[index].x2 - items[index].x1;
225      if(span < 0){ span = 0; } //Make sure it is always positive
226      ret = span +1; //add 1 for the end point
227    }else if(variable == "colspan"){
228      int span = items[index].y2 - items[index].y1;
229      if(span < 0){ span = 0; } //Make sure it is always positive
230      ret = span +1; //add 1 for the end point
231    }
232  }
233  //qDebug() << "Theme:" << item << variable << ret;
234  return ret; //catch for errors;
235}
236
237QString ThemeStruct::styleSheet(){
238  return applicationStyleSheet;
239}
240
241QStringList ThemeStruct::getSpacers(){
242  QStringList spc;
243  QString isvert;
244  for(int i=0; i<itemNames.length(); i++){
245    if( itemNames[i].startsWith("spacer") ){
246      if(items[i].isVertical){ isvert="true"; }
247      else{ isvert="false"; }
248      spc << isvert + "::" + QString::number(items[i].x1) +"::"+ QString::number(items[i].y1);
249    }
250  }
251  //qDebug() << "Spacers:" << spc;
252  return spc;
253}
Note: See TracBrowser for help on using the repository browser.