00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "driver.h"
00021 #include "driveritem.h"
00022
00023 #include <QtCore/QFile>
00024 #include <QtCore/QStringList>
00025 #include <kdebug.h>
00026 #include <klocale.h>
00027 #include <stdlib.h>
00028 #include <math.h>
00029
00030
00031
00032
00033
00034 DrBase::DrBase()
00035 : m_type(DrBase::Base), m_conflict(false)
00036 {
00037 }
00038
00039 DrBase::~DrBase()
00040 {
00041 }
00042
00043 QString DrBase::valueText()
00044 {
00045 return QString();
00046 }
00047
00048 QString DrBase::prettyText()
00049 {
00050 return valueText();
00051 }
00052
00053 void DrBase::setValueText(const QString&)
00054 {
00055 }
00056
00057 DriverItem* DrBase::createItem(DriverItem *parent, DriverItem *after)
00058 {
00059 return new DriverItem(parent, after, this);
00060 }
00061
00062 void DrBase::setOptions(const QMap<QString, QString>& opts)
00063 {
00064 if (opts.contains(name())) setValueText(opts[name()]);
00065 }
00066
00067 void DrBase::getOptions(QMap<QString, QString>& opts, bool incldef)
00068 {
00069 QString val = valueText();
00070 if (incldef || get("persistent") == "1" || get("default") != val)
00071 opts[name()] = val;
00072 }
00073
00074 DrBase* DrBase::clone()
00075 {
00076 DrBase *opt(0);
00077 switch (type()) {
00078 case Main: opt = new DrMain; break;
00079 case Group: opt = new DrGroup; break;
00080 case String: opt = new DrStringOption; break;
00081 case Integer: opt = new DrIntegerOption; break;
00082 case Float: opt = new DrFloatOption; break;
00083 case List: opt = new DrListOption; break;
00084 case Boolean: opt = new DrBooleanOption; break;
00085 default: opt = new DrBase; break;
00086 }
00087 opt->m_map = m_map;
00088 opt->m_name = m_name;
00089 opt->m_conflict = m_conflict;
00090 opt->setValueText(valueText());
00091
00092 return opt;
00093 }
00094
00095
00096
00097
00098
00099 DrMain::DrMain()
00100 : DrGroup()
00101 {
00102 m_type = DrBase::Main;
00103 }
00104
00105 DrMain::~DrMain()
00106 {
00107 qDeleteAll(m_constraints);
00108 qDeleteAll(m_pagesizes);
00109
00110
00111 if (has("temporary"))
00112 QFile::remove(get("temporary"));
00113 }
00114
00115 DriverItem* DrMain::createTreeView(QTreeWidget *parent)
00116 {
00117 DriverItem *root = new DriverItem(parent, this);
00118 root->setExpanded(true);
00119 createTree(root);
00120 return root;
00121 }
00122
00123 int DrMain::checkConstraints()
00124 {
00125 int result(0);
00126 clearConflict();
00127 foreach(DrConstraint* constraint, m_constraints)
00128 if (constraint->check(this))
00129 result++;
00130 return result;
00131 }
00132
00133 void DrMain::addPageSize(DrPageSize *ps)
00134 {
00135 m_pagesizes.insert(ps->pageName(), ps);
00136 }
00137
00138 void DrMain::removeOptionGlobally(const QString& name)
00139 {
00140 DrGroup *grp(0);
00141 DrBase *opt = findOption(name, &grp);
00142
00143 if (opt && grp) {
00144 grp->removeOption(name);
00145 if (grp->isEmpty())
00146 removeGroup(grp);
00147 }
00148 }
00149
00150 void DrMain::removeGroupGlobally(DrGroup *grp)
00151 {
00152 DrGroup *parent(0);
00153 if (findGroup(grp, &parent) && parent) {
00154 parent->removeGroup(grp);
00155 if (parent->isEmpty() && parent != this)
00156 removeGroupGlobally(parent);
00157 }
00158 }
00159
00160 QMap<QString, DrBase*> DrMain::flatten()
00161 {
00162 QMap<QString, DrBase*> optmap;
00163 int index(0);
00164 flattenGroup(optmap, index);
00165 return optmap;
00166 }
00167
00168 DrMain* DrMain::cloneDriver()
00169 {
00170 DrMain *driver = static_cast<DrMain*>(clone());
00171
00172 foreach(DrConstraint* constraint, m_constraints)
00173 driver->addConstraint(new DrConstraint(*constraint));
00174
00175 foreach(DrPageSize* pagesize, m_pagesizes)
00176 driver->addPageSize(new DrPageSize(*pagesize));
00177
00178 return driver;
00179 }
00180
00181
00182
00183
00184
00185 DrGroup::DrGroup()
00186 : DrBase()
00187 {
00188 m_type = DrBase::Group;
00189 }
00190
00191 DrGroup::~DrGroup()
00192 {
00193 qDeleteAll(m_subgroups);
00194 qDeleteAll(m_listoptions);
00195 }
00196
00197 void DrGroup::addOption(DrBase *opt)
00198 {
00199 if (!opt->name().isEmpty()) {
00200 m_options.insert(opt->name(), opt);
00201 m_listoptions.append(opt);
00202 }
00203 }
00204
00205 void DrGroup::addGroup(DrGroup *grp)
00206 {
00207 m_subgroups.append(grp);
00208 }
00209
00210 void DrGroup::addObject(DrBase *optgrp)
00211 {
00212 if (optgrp->isOption())
00213 addOption(optgrp);
00214 else if (optgrp->type() == DrBase::Group)
00215 addGroup(static_cast<DrGroup*>(optgrp));
00216 }
00217
00218 void DrGroup::removeOption(const QString& name)
00219 {
00220 if (m_options.contains(name)) {
00221 DrBase *opt = m_options.take(name);
00222 m_listoptions.removeAll(opt);
00223 delete opt;
00224 }
00225 }
00226
00227 void DrGroup::removeGroup(DrGroup *grp)
00228 {
00229 m_subgroups.removeAll(grp);
00230 delete grp;
00231 }
00232
00233 bool DrGroup::isEmpty()
00234 {
00235 return m_options.isEmpty() && m_subgroups.isEmpty();
00236 }
00237
00238 DriverItem* DrGroup::createItem(DriverItem *parent, DriverItem *after)
00239 {
00240 DriverItem *item = DrBase::createItem(parent, after);
00241 createTree(item);
00242 return item;
00243 }
00244
00245 void DrGroup::createTree(DriverItem *parent)
00246 {
00247 DriverItem *item(0);
00248
00249 foreach(DrGroup* subgroup, m_subgroups) {
00250 item = subgroup->createItem(parent, item);
00251 item->setExpanded(true);
00252 }
00253
00254 foreach(DrBase* option, m_listoptions)
00255 item = option->createItem(parent, item);
00256 }
00257
00258 DrBase* DrGroup::findOption(const QString& name, DrGroup **parentGroup)
00259 {
00260 DrBase *opt(0);
00261 if (m_options.contains(name)) {
00262 opt = m_options.value(name);
00263 if (parentGroup)
00264 *parentGroup = this;
00265 } else {
00266 QListIterator<DrGroup*> it(m_subgroups);
00267 while (it.hasNext() && !opt)
00268 opt = it.next()->findOption(name, parentGroup);
00269 }
00270 return opt;
00271 }
00272
00273 DrGroup* DrGroup::findGroup(DrGroup *grp, DrGroup ** parentGroup)
00274 {
00275 DrGroup *group(0);
00276 if (m_subgroups.contains(grp)) {
00277 group = grp;
00278 if (parentGroup)
00279 *parentGroup = this;
00280 } else {
00281 QListIterator<DrGroup*> it(m_subgroups);
00282 while (it.hasNext() && !group)
00283 group = it.next()->findGroup(grp, parentGroup);
00284 }
00285 return group;
00286 }
00287
00288 void DrGroup::clearConflict()
00289 {
00290 foreach(DrBase* option, m_options)
00291 option->setConflict(false);
00292
00293 foreach(DrGroup* subgroup, m_subgroups)
00294 subgroup->clearConflict();
00295 }
00296
00297 void DrGroup::setOptions(const QMap<QString, QString>& opts)
00298 {
00299 foreach(DrBase* option, m_options)
00300 option->setOptions(opts);
00301
00302 foreach(DrGroup* subgroup, m_subgroups)
00303 subgroup->setOptions(opts);
00304 }
00305
00306 void DrGroup::getOptions(QMap<QString, QString>& opts, bool incldef)
00307 {
00308 foreach(DrBase* option, m_options)
00309 option->getOptions(opts, incldef);
00310
00311 foreach(DrGroup* subgroup, m_subgroups)
00312 subgroup->getOptions(opts, incldef);
00313 }
00314
00315 void DrGroup::flattenGroup(QMap<QString, DrBase*>& optmap, int& index)
00316 {
00317 foreach(DrGroup* subgroup, m_subgroups)
00318 subgroup->flattenGroup(optmap, index);
00319
00320 foreach(DrBase* option, m_options)
00321 optmap[option->name()] = option;
00322
00323 if (name().isEmpty())
00324 optmap[QString::fromLatin1("group%1").arg(index++)] = this;
00325 else
00326 optmap[name()] = this;
00327
00328 m_subgroups.clear();
00329 m_options.clear();
00330 qDeleteAll(m_listoptions);
00331 m_listoptions.clear();
00332 }
00333
00334 DrBase* DrGroup::clone()
00335 {
00336 DrGroup *grp = static_cast<DrGroup*>(DrBase::clone());
00337
00338 foreach(DrGroup* subgroup, m_subgroups)
00339 grp->addGroup(static_cast<DrGroup*>(subgroup->clone()));
00340
00341 foreach(DrBase* option, m_listoptions)
00342 grp->addOption(option->clone());
00343
00344 return static_cast<DrBase*>(grp);
00345 }
00346
00347 QString DrGroup::groupForOption(const QString& optname)
00348 {
00349 QString grpname;
00350 if (optname == "PageSize" ||
00351 optname == "InputSlot" ||
00352 optname == "ManualFeed" ||
00353 optname == "MediaType" ||
00354 optname == "MediaColor" ||
00355 optname == "MediaWeight" ||
00356 optname == "Duplex" ||
00357 optname == "DoubleSided" ||
00358 optname == "Copies")
00359 grpname = i18n("General");
00360 else if (optname.startsWith("stp") ||
00361 optname == "Cyan" ||
00362 optname == "Yellow" ||
00363 optname == "Magenta" ||
00364 optname == "Black" ||
00365 optname == "Density" ||
00366 optname == "Contrast")
00367 grpname = i18n("Adjustments");
00368 else if (optname.startsWith("JCL"))
00369 grpname = i18n("JCL");
00370 else
00371 grpname = i18n("Others");
00372 return grpname;
00373 }
00374
00375
00376
00377
00378
00379 DrChoiceGroup::DrChoiceGroup()
00380 : DrGroup()
00381 {
00382 m_type = DrBase::ChoiceGroup;
00383 }
00384
00385 DrChoiceGroup::~DrChoiceGroup()
00386 {
00387 }
00388
00389 DriverItem* DrChoiceGroup::createItem(DriverItem *parent, DriverItem*)
00390 {
00391 createTree(parent);
00392 return NULL;
00393 }
00394
00395
00396
00397
00398
00399 DrStringOption::DrStringOption()
00400 : DrBase()
00401 {
00402 m_type = DrBase::String;
00403 }
00404
00405 DrStringOption::~DrStringOption()
00406 {
00407 }
00408
00409 QString DrStringOption::valueText()
00410 {
00411 return m_value;
00412 }
00413
00414 void DrStringOption::setValueText(const QString& s)
00415 {
00416 m_value = s;
00417 }
00418
00419
00420
00421
00422
00423 DrIntegerOption::DrIntegerOption()
00424 : DrBase()
00425 {
00426 m_type = DrBase::Integer;
00427 m_value = 0;
00428 set("minval", "0");
00429 set("maxval", "10");
00430 }
00431
00432 DrIntegerOption::~DrIntegerOption()
00433 {
00434 }
00435
00436 QString DrIntegerOption::valueText()
00437 {
00438 QString s = QString::number(m_value);
00439 return s;
00440 }
00441
00442 void DrIntegerOption::setValueText(const QString& s)
00443 {
00444 m_value = s.toInt();
00445 }
00446
00447 QString DrIntegerOption::fixedVal()
00448 {
00449 QStringList vals = get("fixedvals").split("|", QString::SkipEmptyParts);
00450 if (vals.count() == 0)
00451 return valueText();
00452 int d(0);
00453 QString val;
00454 for (QStringList::Iterator it = vals.begin(); it != vals.end(); ++it) {
00455 int thisVal = (*it).toInt();
00456 if (val.isEmpty() || abs(thisVal - m_value) < d) {
00457 d = abs(thisVal - m_value);
00458 val = *it;
00459 }
00460 }
00461 if (val.isEmpty())
00462 return valueText();
00463 else
00464 return val;
00465 }
00466
00467
00468
00469
00470
00471 DrFloatOption::DrFloatOption()
00472 : DrBase()
00473 {
00474 m_type = DrBase::Float;
00475 m_value = 0.0;
00476 set("minval", "0.0");
00477 set("maxval", "1.0");
00478 }
00479
00480 DrFloatOption::~DrFloatOption()
00481 {
00482 }
00483
00484 QString DrFloatOption::valueText()
00485 {
00486 QString s = QString::number(m_value, 'f', 3);
00487 return s;
00488 }
00489
00490 void DrFloatOption::setValueText(const QString& s)
00491 {
00492 m_value = s.toFloat();
00493 }
00494
00495 QString DrFloatOption::fixedVal()
00496 {
00497 QStringList vals = get("fixedvals").split("|", QString::SkipEmptyParts);
00498 if (vals.count() == 0)
00499 return valueText();
00500 float d(0);
00501 QString val;
00502 for (QStringList::Iterator it = vals.begin(); it != vals.end(); ++it) {
00503 float thisVal = (*it).toFloat();
00504 if (val.isEmpty() || fabs(thisVal - m_value) < d) {
00505 d = fabs(thisVal - m_value);
00506 val = *it;
00507 }
00508 }
00509 if (val.isEmpty())
00510 return valueText();
00511 else
00512 return val;
00513 }
00514
00515
00516
00517
00518
00519 DrListOption::DrListOption()
00520 : DrBase()
00521 {
00522 m_type = DrBase::List;
00523 m_current = 0;
00524 }
00525
00526 DrListOption::~DrListOption()
00527 {
00528 qDeleteAll(m_choices);
00529 }
00530
00531 QString DrListOption::valueText()
00532 {
00533 if (m_current)
00534 return m_current->name();
00535 else
00536 return QString();
00537 }
00538
00539 QString DrListOption::prettyText()
00540 {
00541 if (m_current)
00542 return m_current->get("text");
00543 else
00544 return QString();
00545 }
00546
00547 void DrListOption::setValueText(const QString& s)
00548 {
00549 m_current = findChoice(s);
00550 if (!m_current) {
00551 bool ok;
00552 int index = s.toInt(&ok);
00553 if (ok)
00554 setChoice(index);
00555 }
00556 }
00557
00558 DrBase* DrListOption::findChoice(const QString& txt)
00559 {
00560 foreach(DrBase* choice, m_choices)
00561 if (choice->name() == txt)
00562 return choice;
00563 return 0;
00564 }
00565
00566 DrBase* DrListOption::clone()
00567 {
00568 DrListOption *opt = static_cast<DrListOption*>(DrBase::clone());
00569
00570 foreach(DrBase* choice, m_choices)
00571 opt->addChoice(choice->clone());
00572
00573 opt->setValueText(valueText());
00574
00575 return static_cast<DrBase*>(opt);
00576 }
00577
00578 void DrListOption::getOptions(QMap<QString, QString>& opts, bool incldef)
00579 {
00580 DrBase::getOptions(opts, incldef);
00581 if (currentChoice() && currentChoice()->type() == DrBase::ChoiceGroup)
00582 currentChoice()->getOptions(opts, incldef);
00583 }
00584
00585 void DrListOption::setOptions(const QMap<QString, QString>& opts)
00586 {
00587 DrBase::setOptions(opts);
00588 if (currentChoice() && currentChoice()->type() == DrBase::ChoiceGroup)
00589 currentChoice()->setOptions(opts);
00590 }
00591
00592 DriverItem* DrListOption::createItem(DriverItem *parent, DriverItem *after)
00593 {
00594 DriverItem *item = DrBase::createItem(parent, after);
00595
00596
00597
00598
00599 return item;
00600 }
00601
00602 void DrListOption::setChoice(int choicenum)
00603 {
00604 if (choicenum >= 0 && choicenum < (int)m_choices.count()) {
00605 setValueText(m_choices.at(choicenum)->name());
00606 }
00607 }
00608
00609
00610
00611
00612
00613 DrConstraint::DrConstraint(const QString& o1, const QString& o2, const QString& c1, const QString& c2)
00614 : m_opt1(o1), m_opt2(o2), m_choice1(c1), m_choice2(c2), m_option1(0), m_option2(0)
00615 {
00616 }
00617
00618 DrConstraint::DrConstraint(const DrConstraint& d)
00619 : m_opt1(d.m_opt1), m_opt2(d.m_opt2), m_choice1(d.m_choice1), m_choice2(d.m_choice2), m_option1(0), m_option2(0)
00620 {
00621 }
00622
00623 bool DrConstraint::check(DrMain *driver)
00624 {
00625 if (!m_option1) m_option1 = (DrListOption*)driver->findOption(m_opt1);
00626 if (!m_option2) m_option2 = (DrListOption*)driver->findOption(m_opt2);
00627 if (m_option1 && m_option2 && m_option1->currentChoice() && m_option2->currentChoice()) {
00628 bool f1(false), f2(false);
00629 QString c1(m_option1->currentChoice()->name()), c2(m_option2->currentChoice()->name());
00630
00631 if (m_choice1.isEmpty())
00632 f1 = (c1 != "None" && c1 != "Off" && c1 != "False");
00633 else
00634 f1 = (c1 == m_choice1);
00635 if (m_choice2.isEmpty())
00636 f2 = (c2 != "None" && c2 != "Off" && c2 != "False");
00637 else
00638 f2 = (c2 == m_choice2);
00639
00640 QString s((f1 && f2 ? "1" : "0"));
00641 if (!m_option1->conflict()) m_option1->setConflict(f1 && f2);
00642 if (!m_option2->conflict()) m_option2->setConflict(f1 && f2);
00643
00644 return (f1 && f2);
00645 }
00646 return false;
00647 }
00648
00649
00650
00651
00652
00653 DrPageSize::DrPageSize(const QString& s, float width, float height, float left, float bottom, float right, float top)
00654 : m_name(s),
00655 m_width(width),
00656 m_height(height),
00657 m_left(left),
00658 m_bottom(bottom),
00659 m_right(right),
00660 m_top(top)
00661 {
00662 }
00663
00664 DrPageSize::DrPageSize(const DrPageSize& d)
00665 : m_name(d.m_name),
00666 m_width(d.m_width),
00667 m_height(d.m_height),
00668 m_left(d.m_left),
00669 m_bottom(d.m_bottom),
00670 m_right(d.m_right),
00671 m_top(d.m_top)
00672 {
00673 }
00674
00675 QSize DrPageSize::pageSize() const
00676 {
00677 return QSize((int)m_width, (int)m_height);
00678 }
00679
00680 QRect DrPageSize::pageRect() const
00681 {
00682 return QRect((int)(m_left + 0.5), (int)(m_top + 0.5), (int)(m_width - m_left - m_right), (int)(m_height - m_top - m_bottom));
00683 }
00684
00685 QSize DrPageSize::margins() const
00686 {
00687 return QSize((int)(m_left + 0.5), (int)(m_top + 0.5));
00688 }