QTfrontend/ammoSchemeModel.cpp
changeset 1897 e9dcb47013c7
parent 1895 7ba647a88b2f
child 1899 5763f46d7486
equal deleted inserted replaced
1896:fdacad5d0acc 1897:e9dcb47013c7
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    17  */
    17  */
    18 
    18 
    19 #include <QDebug>
    19 #include <QDebug>
    20 #include <QModelIndex>
    20 #include <QModelIndex>
       
    21 
    21 #include "ammoSchemeModel.h"
    22 #include "ammoSchemeModel.h"
       
    23 #include "hwconsts.h"
    22 
    24 
    23 AmmoSchemeModel::AmmoSchemeModel(QObject* parent) :
    25 AmmoSchemeModel::AmmoSchemeModel(QObject* parent, const QString & fileName) :
    24   QAbstractTableModel(parent)
    26 	QAbstractTableModel(parent),
       
    27 	fileConfig(fileName, QSettings::IniFormat)
    25 {
    28 {
       
    29 	spNames
       
    30 		<< "name"             //  0
       
    31 		<< "fortsmode"        //  1
       
    32 		<< "divteams"         //  2
       
    33 		<< "solidland"        //  3
       
    34 		<< "border"           //  4
       
    35 		<< "lowgrav"          //  5
       
    36 		<< "laser"            //  6
       
    37 		<< "invulnerability"  //  7
       
    38 		<< "mines"            //  8
       
    39 		<< "damagefactor"     //  9
       
    40 		<< "turntime"         // 10
       
    41 		<< "health"           // 11
       
    42 		<< "suddendeath"      // 12
       
    43 		<< "caseprobability"  // 13
       
    44 		;
       
    45 
       
    46 
       
    47 	QStringList predefSchemesNames;
       
    48 	predefSchemesNames
       
    49 		<< tr("Default")
       
    50 		<< tr("Pro mode");
       
    51 	
    26 	defaultScheme
    52 	defaultScheme
    27 		<< QVariant(tr("Default")) // name           0
    53 		<< QVariant(tr("Default")) // name           0
    28 		<< QVariant(false)         // fortsmode      1
    54 		<< QVariant(false)         // fortsmode      1
    29 		<< QVariant(false)         // team divide    2
    55 		<< QVariant(false)         // team divide    2
    30 		<< QVariant(false)         // solid land     3
    56 		<< QVariant(false)         // solid land     3
    38 		<< QVariant(100)           // init health    11
    64 		<< QVariant(100)           // init health    11
    39 		<< QVariant(15)            // sudden death   12
    65 		<< QVariant(15)            // sudden death   12
    40 		<< QVariant(5)             // case prob      13
    66 		<< QVariant(5)             // case prob      13
    41 		;
    67 		;
    42 
    68 
       
    69 	QList<QVariant> proMode;
       
    70 	proMode
       
    71 		<< QVariant(tr("Pro mode"))// name           0
       
    72 		<< QVariant(false)         // fortsmode      1
       
    73 		<< QVariant(false)         // team divide    2
       
    74 		<< QVariant(false)         // solid land     3
       
    75 		<< QVariant(false)         // border         4
       
    76 		<< QVariant(false)         // low gravity    5
       
    77 		<< QVariant(false)         // laser sight    6
       
    78 		<< QVariant(false)         // invulnerable   7
       
    79 		<< QVariant(false)         // add mines      8
       
    80 		<< QVariant(100)           // damage modfier 9
       
    81 		<< QVariant(15)            // turn time      10
       
    82 		<< QVariant(100)           // init health    11
       
    83 		<< QVariant(15)            // sudden death   12
       
    84 		<< QVariant(0)             // case prob      13
       
    85 		;
       
    86 
    43 	schemes.append(defaultScheme);
    87 	schemes.append(defaultScheme);
       
    88 	schemes.append(proMode);
       
    89 
       
    90 
       
    91 	int size = fileConfig.beginReadArray("schemes");
       
    92 	for (int i = 0; i < size; ++i) {
       
    93 		fileConfig.setArrayIndex(i);
       
    94 
       
    95 		if (!predefSchemesNames.contains(fileConfig.value(spNames[0]).toString()))
       
    96 		{
       
    97 			QList<QVariant> scheme;
       
    98 
       
    99 			for (int k = 0; k < spNames.size(); ++k)
       
   100 				scheme << fileConfig.value(spNames[k], defaultScheme[k]);
       
   101 
       
   102 			schemes.append(scheme);
       
   103 		}
       
   104 	}
       
   105 	fileConfig.endArray();
    44 }
   106 }
    45 
   107 
    46 QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const
   108 QVariant AmmoSchemeModel::headerData(int section, Qt::Orientation orientation, int role) const
    47 {
   109 {
    48 	return QVariant();
   110 	return QVariant();
   100 
   162 
   101 bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent)
   163 bool AmmoSchemeModel::removeRows(int row, int count, const QModelIndex & parent)
   102 {
   164 {
   103 	beginRemoveRows(parent, row, row);
   165 	beginRemoveRows(parent, row, row);
   104 
   166 
       
   167 	fileConfig.remove(schemes[row][0].toString());
   105 	schemes.removeAt(row);
   168 	schemes.removeAt(row);
   106 
   169 
   107 	endRemoveRows();
   170 	endRemoveRows();
   108 }
   171 }
   109 
   172 
   116 		)
   179 		)
   117 		return QVariant();
   180 		return QVariant();
   118 
   181 
   119 	return schemes[index.row()][index.column()];
   182 	return schemes[index.row()][index.column()];
   120 }
   183 }
       
   184 
       
   185 void AmmoSchemeModel::Save()
       
   186 {
       
   187 	fileConfig.beginWriteArray("schemes");
       
   188 	for (int i = 0; i < schemes.size(); ++i) {
       
   189 		fileConfig.setArrayIndex(i);
       
   190 
       
   191 		QList<QVariant> scheme = schemes[i];
       
   192 
       
   193 		for (int k = 0; k < spNames.size(); ++k)
       
   194 			fileConfig.setValue(spNames[k], scheme[k]);
       
   195 	}
       
   196 	fileConfig.endArray();
       
   197 }