• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kdeprint
 

kdeprint

  • kdeprint
driver.cpp
1 /*
2  * This file is part of the KDE libraries
3  * Copyright (c) 2001 Michael Goffioul <kdeprint@swing.be>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "driver.h"
21 #include "driveritem.h"
22 
23 #include <tqfile.h>
24 #include <tqstringlist.h>
25 #include <kdebug.h>
26 #include <klocale.h>
27 #include <stdlib.h>
28 #include <math.h>
29 
30 /******************
31  * DrBase members *
32  ******************/
33 
34 DrBase::DrBase()
35 : m_type(DrBase::Base), m_conflict(false)
36 {
37 }
38 
39 DrBase::~DrBase()
40 {
41 }
42 
43 TQString DrBase::valueText()
44 {
45  return TQString::null;
46 }
47 
48 TQString DrBase::prettyText()
49 {
50  return valueText();
51 }
52 
53 void DrBase::setValueText(const TQString&)
54 {
55 }
56 
57 DriverItem* DrBase::createItem(DriverItem *parent, DriverItem *after)
58 {
59  return new DriverItem(parent, after, this);
60 }
61 
62 void DrBase::setOptions(const TQMap<TQString,TQString>& opts)
63 {
64  if (opts.contains(name())) setValueText(opts[name()]);
65 }
66 
67 void DrBase::getOptions(TQMap<TQString,TQString>& opts, bool incldef)
68 {
69  QString val = valueText();
70  if ( incldef || get( "persistent" ) == "1" || get("default") != val )
71  opts[name()] = val;
72 }
73 
74 DrBase* DrBase::clone()
75 {
76  DrBase *opt(0);
77  switch (type())
78  {
79  case Main: opt = new DrMain; break;
80  case Group: opt = new DrGroup; break;
81  case String: opt = new DrStringOption; break;
82  case Integer: opt = new DrIntegerOption; break;
83  case Float: opt = new DrFloatOption; break;
84  case List: opt = new DrListOption; break;
85  case Boolean: opt = new DrBooleanOption; break;
86  default: opt = new DrBase; break;
87  }
88  opt->m_map = m_map;
89  opt->m_name = m_name;
90  opt->m_conflict = m_conflict;
91  opt->setValueText(valueText());
92 
93  return opt;
94 }
95 
96 /******************
97  * DrMain members *
98  ******************/
99 
100 DrMain::DrMain()
101 : DrGroup()
102 {
103  m_type = DrBase::Main;
104  m_constraints.setAutoDelete(true);
105  m_pagesizes.setAutoDelete(true);
106 }
107 
108 DrMain::~DrMain()
109 {
110  // remove a possible temporary file
111  if (has("temporary"))
112  TQFile::remove(get("temporary"));
113 }
114 
115 DriverItem* DrMain::createTreeView(TQListView *parent)
116 {
117  DriverItem *root = new DriverItem(parent, this);
118  createTree(root);
119  return root;
120 }
121 
122 int DrMain::checkConstraints()
123 {
124  int result(0);
125  clearConflict();
126  TQPtrListIterator<DrConstraint> it(m_constraints);
127  for (;it.current();++it)
128  if (it.current()->check(this))
129  result++;
130  return result;
131 }
132 
133 void DrMain::addPageSize(DrPageSize *ps)
134 {
135  m_pagesizes.insert(ps->pageName(),ps);
136 }
137 
138 void DrMain::removeOptionGlobally(const TQString& name)
139 {
140  DrGroup *grp(0);
141  DrBase *opt = findOption(name, &grp);
142 
143  if (opt && grp)
144  {
145  grp->removeOption(name);
146  if (grp->isEmpty())
147  removeGroup(grp);
148  }
149 }
150 
151 void DrMain::removeGroupGlobally(DrGroup *grp)
152 {
153  DrGroup *parent(0);
154  if (findGroup(grp, &parent) && parent)
155  {
156  parent->removeGroup(grp);
157  if (parent->isEmpty() && parent != this)
158  removeGroupGlobally(parent);
159  }
160 }
161 
162 TQMap<TQString, DrBase*> DrMain::flatten()
163 {
164  TQMap<TQString, DrBase*> optmap;
165  int index(0);
166  flattenGroup(optmap, index);
167  return optmap;
168 }
169 
170 DrMain* DrMain::cloneDriver()
171 {
172  DrMain *driver = static_cast<DrMain*>(clone());
173 
174  TQPtrListIterator<DrConstraint> cit(m_constraints);
175  for (; cit.current(); ++cit)
176  driver->addConstraint(new DrConstraint(*(cit.current())));
177 
178  TQDictIterator<DrPageSize> pit(m_pagesizes);
179  for (; pit.current(); ++pit)
180  driver->addPageSize(new DrPageSize(*(pit.current())));
181 
182  return driver;
183 }
184 
185 /*******************
186  * DrGroup members *
187  *******************/
188 
189 DrGroup::DrGroup()
190 : DrBase()
191 {
192  m_type = DrBase::Group;
193 
194  m_subgroups.setAutoDelete(true);
195  m_options.setAutoDelete(true);
196  m_listoptions.setAutoDelete(false);
197 }
198 
199 DrGroup::~DrGroup()
200 {
201 }
202 
203 void DrGroup::addOption(DrBase *opt)
204 {
205  if (!opt->name().isEmpty())
206  {
207  m_options.insert(opt->name(),opt);
208  m_listoptions.append(opt);
209  }
210 }
211 
212 void DrGroup::addGroup(DrGroup *grp)
213 {
214  m_subgroups.append(grp);
215 }
216 
217 void DrGroup::addObject(DrBase *optgrp)
218 {
219  if (optgrp->isOption())
220  addOption(optgrp);
221  else if (optgrp->type() == DrBase::Group)
222  addGroup(static_cast<DrGroup*>(optgrp));
223 }
224 
225 void DrGroup::removeOption(const TQString& name)
226 {
227  DrBase *opt = m_options.find(name);
228  if (opt)
229  {
230  m_listoptions.removeRef(opt);
231  m_options.remove(name);
232  }
233 }
234 
235 void DrGroup::removeGroup(DrGroup *grp)
236 {
237  m_subgroups.removeRef(grp);
238 }
239 
240 bool DrGroup::isEmpty()
241 {
242  return (m_options.count()+m_subgroups.count() == 0);
243 }
244 
245 DriverItem* DrGroup::createItem(DriverItem *parent, DriverItem *after)
246 {
247  DriverItem *item = DrBase::createItem(parent, after);
248  createTree(item);
249  return item;
250 }
251 
252 void DrGroup::createTree(DriverItem *parent)
253 {
254  DriverItem *item(0);
255 
256  TQPtrListIterator<DrGroup> lit(m_subgroups);
257  for (;lit.current();++lit)
258  item = lit.current()->createItem(parent, item);
259 
260  TQPtrListIterator<DrBase> dit(m_listoptions);
261  for (;dit.current();++dit)
262  item = dit.current()->createItem(parent, item);
263 }
264 
265 DrBase* DrGroup::findOption(const TQString& name, DrGroup **parentGroup)
266 {
267  DrBase *opt = m_options.find(name);
268  if (!opt)
269  {
270  TQPtrListIterator<DrGroup> it(m_subgroups);
271  for (;it.current() && !opt; ++it)
272  opt = it.current()->findOption(name, parentGroup);
273  }
274  else if (parentGroup)
275  *parentGroup = this;
276  return opt;
277 }
278 
279 DrGroup* DrGroup::findGroup(DrGroup *grp, DrGroup ** parentGroup)
280 {
281  DrGroup *group = (m_subgroups.findRef(grp) == -1 ? 0 : grp);
282  if (!group)
283  {
284  TQPtrListIterator<DrGroup> it(m_subgroups);
285  for (;it.current() && !group; ++it)
286  group = it.current()->findGroup(grp, parentGroup);
287  }
288  else if (parentGroup)
289  *parentGroup = this;
290  return group;
291 }
292 
293 void DrGroup::clearConflict()
294 {
295  TQDictIterator<DrBase> dit(m_options);
296  for (;dit.current();++dit)
297  dit.current()->setConflict(false);
298 
299  TQPtrListIterator<DrGroup> lit(m_subgroups);
300  for (;lit.current();++lit)
301  lit.current()->clearConflict();
302 }
303 
304 void DrGroup::setOptions(const TQMap<TQString,TQString>& opts)
305 {
306  TQDictIterator<DrBase> dit(m_options);
307  for (;dit.current();++dit)
308  dit.current()->setOptions(opts);
309 
310  TQPtrListIterator<DrGroup> lit(m_subgroups);
311  for (;lit.current();++lit)
312  lit.current()->setOptions(opts);
313 }
314 
315 void DrGroup::getOptions(TQMap<TQString,TQString>& opts, bool incldef)
316 {
317  TQDictIterator<DrBase> dit(m_options);
318  for (;dit.current();++dit)
319  dit.current()->getOptions(opts,incldef);
320 
321  TQPtrListIterator<DrGroup> lit(m_subgroups);
322  for (;lit.current();++lit)
323  lit.current()->getOptions(opts,incldef);
324 }
325 
326 void DrGroup::flattenGroup(TQMap<TQString, DrBase*>& optmap, int& index)
327 {
328  TQPtrListIterator<DrGroup> git(m_subgroups);
329  for (; git.current(); ++git)
330  git.current()->flattenGroup(optmap, index);
331 
332  TQDictIterator<DrBase> oit(m_options);
333  for (; oit.current(); ++oit)
334  optmap[oit.current()->name()] = oit.current();
335 
336  if (name().isEmpty())
337  optmap[TQString::fromLatin1("group%1").arg(index++)] = this;
338  else
339  optmap[name()] = this;
340 
341  m_subgroups.setAutoDelete(false);
342  m_options.setAutoDelete(false);
343  m_subgroups.clear();
344  m_options.clear();
345  m_listoptions.clear();
346  m_subgroups.setAutoDelete(true);
347  m_options.setAutoDelete(true);
348 }
349 
350 DrBase* DrGroup::clone()
351 {
352  DrGroup *grp = static_cast<DrGroup*>(DrBase::clone());
353 
354  TQPtrListIterator<DrGroup> git(m_subgroups);
355  for (; git.current(); ++git)
356  grp->addGroup(static_cast<DrGroup*>(git.current()->clone()));
357 
358  TQPtrListIterator<DrBase> oit(m_listoptions);
359  for (; oit.current(); ++oit)
360  grp->addOption(oit.current()->clone());
361 
362  return static_cast<DrBase*>(grp);
363 }
364 
365 TQString DrGroup::groupForOption( const TQString& optname )
366 {
367  TQString grpname;
368  if ( optname == "PageSize" ||
369  optname == "InputSlot" ||
370  optname == "ManualFeed" ||
371  optname == "MediaType" ||
372  optname == "MediaColor" ||
373  optname == "MediaWeight" ||
374  optname == "Duplex" ||
375  optname == "DoubleSided" ||
376  optname == "Copies" )
377  grpname = i18n( "General" );
378  else if ( optname.startsWith( "stp" ) ||
379  optname == "Cyan" ||
380  optname == "Yellow" ||
381  optname == "Magenta" ||
382  optname == "Black" ||
383  optname == "Density" ||
384  optname == "Contrast" )
385  grpname = i18n( "Adjustments" );
386  else if ( optname.startsWith( "JCL" ) )
387  grpname = i18n( "JCL" );
388  else
389  grpname = i18n( "Others" );
390  return grpname;
391 }
392 
393 /*************************
394  * DrChoiceGroup members *
395  *************************/
396 
397 DrChoiceGroup::DrChoiceGroup()
398 : DrGroup()
399 {
400  m_type = DrBase::ChoiceGroup;
401 }
402 
403 DrChoiceGroup::~DrChoiceGroup()
404 {
405 }
406 
407 DriverItem* DrChoiceGroup::createItem(DriverItem *parent, DriverItem*)
408 {
409  createTree(parent);
410  return NULL;
411 }
412 
413 /**************************
414  * DrStringOption members *
415  **************************/
416 
417 DrStringOption::DrStringOption()
418 : DrBase()
419 {
420  m_type = DrBase::String;
421 }
422 
423 DrStringOption::~DrStringOption()
424 {
425 }
426 
427 TQString DrStringOption::valueText()
428 {
429  return m_value;
430 }
431 
432 void DrStringOption::setValueText(const TQString& s)
433 {
434  m_value = s;
435 }
436 
437 /***************************
438  * DrIntegerOption members *
439  ***************************/
440 
441 DrIntegerOption::DrIntegerOption()
442 : DrBase()
443 {
444  m_type = DrBase::Integer;
445  m_value = 0;
446  set("minval","0");
447  set("maxval","10");
448 }
449 
450 DrIntegerOption::~DrIntegerOption()
451 {
452 }
453 
454 TQString DrIntegerOption::valueText()
455 {
456  QString s = TQString::number(m_value);
457  return s;
458 }
459 
460 void DrIntegerOption::setValueText(const TQString& s)
461 {
462  m_value = s.toInt();
463 }
464 
465 TQString DrIntegerOption::fixedVal()
466 {
467  TQStringList vals = TQStringList::split("|", get("fixedvals"), false);
468  if (vals.count() == 0)
469  return valueText();
470  int d(0);
471  TQString val;
472  for (TQStringList::Iterator it=vals.begin(); it!=vals.end(); ++it)
473  {
474  int thisVal = (*it).toInt();
475  if (val.isEmpty() || abs(thisVal - m_value) < d)
476  {
477  d = abs(thisVal - m_value);
478  val = *it;
479  }
480  }
481  if (val.isEmpty())
482  return valueText();
483  else
484  return val;
485 }
486 
487 /*************************
488  * DrFloatOption members *
489  *************************/
490 
491 DrFloatOption::DrFloatOption()
492 : DrBase()
493 {
494  m_type = DrBase::Float;
495  m_value = 0.0;
496  set("minval","0.0");
497  set("maxval","1.0");
498 }
499 
500 DrFloatOption::~DrFloatOption()
501 {
502 }
503 
504 TQString DrFloatOption::valueText()
505 {
506  QString s = TQString::number(m_value,'f',3);
507  return s;
508 }
509 
510 void DrFloatOption::setValueText(const TQString& s)
511 {
512  m_value = s.toFloat();
513 }
514 
515 TQString DrFloatOption::fixedVal()
516 {
517  TQStringList vals = TQStringList::split("|", get("fixedvals"), false);
518  if (vals.count() == 0)
519  return valueText();
520  float d(0);
521  TQString val;
522  for (TQStringList::Iterator it=vals.begin(); it!=vals.end(); ++it)
523  {
524  float thisVal = (*it).toFloat();
525  if (val.isEmpty() || fabs(thisVal - m_value) < d)
526  {
527  d = fabs(thisVal - m_value);
528  val = *it;
529  }
530  }
531  if (val.isEmpty())
532  return valueText();
533  else
534  return val;
535 }
536 
537 /************************
538  * DrListOption members *
539  ************************/
540 
541 DrListOption::DrListOption()
542 : DrBase()
543 {
544  m_type = DrBase::List;
545 
546  m_choices.setAutoDelete(true);
547  m_current = 0;
548 }
549 
550 DrListOption::~DrListOption()
551 {
552 }
553 
554 TQString DrListOption::valueText()
555 {
556  QString s = (m_current ? m_current->name() : TQString::null);
557  return s;
558 }
559 
560 TQString DrListOption::prettyText()
561 {
562  if (m_current)
563  return m_current->get("text");
564  else
565  return TQString::null;
566 }
567 
568 void DrListOption::setValueText(const TQString& s)
569 {
570  m_current = findChoice(s);
571  if (!m_current)
572  {
573  bool ok;
574  int index = s.toInt(&ok);
575  if (ok)
576  setChoice(index);
577  }
578 }
579 
580 DrBase* DrListOption::findChoice(const TQString& txt)
581 {
582  TQPtrListIterator<DrBase> it(m_choices);
583  for (;it.current();++it)
584  if (it.current()->name() == txt)
585  return it.current();
586  return NULL;
587 }
588 
589 DrBase* DrListOption::clone()
590 {
591  DrListOption *opt = static_cast<DrListOption*>(DrBase::clone());
592 
593  TQPtrListIterator<DrBase> it(m_choices);
594  for (; it.current(); ++it)
595  opt->addChoice(it.current()->clone());
596 
597  opt->setValueText(valueText());
598 
599  return static_cast<DrBase*>(opt);
600 }
601 
602 void DrListOption::getOptions(TQMap<TQString,TQString>& opts, bool incldef)
603 {
604  DrBase::getOptions(opts, incldef);
605  if (currentChoice() && currentChoice()->type() == DrBase::ChoiceGroup)
606  currentChoice()->getOptions(opts, incldef);
607 }
608 
609 void DrListOption::setOptions(const TQMap<TQString,TQString>& opts)
610 {
611  DrBase::setOptions(opts);
612  if (currentChoice() && currentChoice()->type() == DrBase::ChoiceGroup)
613  currentChoice()->setOptions(opts);
614 }
615 
616 DriverItem* DrListOption::createItem(DriverItem *parent, DriverItem *after)
617 {
618  DriverItem *item = DrBase::createItem(parent, after);
619  /*if (currentChoice() && currentChoice()->type() == DrBase::ChoiceGroup)
620  {
621  currentChoice()->createItem(item);
622  }*/
623  return item;
624 }
625 
626 void DrListOption::setChoice(int choicenum)
627 {
628  if (choicenum >= 0 && choicenum < (int)m_choices.count())
629  {
630  setValueText(m_choices.at(choicenum)->name());
631  }
632 }
633 
634 /************************
635  * DrConstraint members *
636  ************************/
637 
638 DrConstraint::DrConstraint(const TQString& o1, const TQString& o2, const TQString& c1, const TQString& c2)
639 : m_opt1(o1), m_opt2(o2), m_choice1(c1), m_choice2(c2), m_option1(0), m_option2(0)
640 {
641 }
642 
643 DrConstraint::DrConstraint(const DrConstraint& d)
644 : 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)
645 {
646 }
647 
648 bool DrConstraint::check(DrMain *driver)
649 {
650  if (!m_option1) m_option1 = (DrListOption*)driver->findOption(m_opt1);
651  if (!m_option2) m_option2 = (DrListOption*)driver->findOption(m_opt2);
652  if (m_option1 && m_option2 && m_option1->currentChoice() && m_option2->currentChoice())
653  {
654  bool f1(false), f2(false);
655  QString c1(m_option1->currentChoice()->name()), c2(m_option2->currentChoice()->name());
656  // check choices
657  if (m_choice1.isEmpty())
658  f1 = (c1 != "None" && c1 != "Off" && c1 != "False");
659  else
660  f1 = (c1 == m_choice1);
661  if (m_choice2.isEmpty())
662  f2 = (c2 != "None" && c2 != "Off" && c2 != "False");
663  else
664  f2 = (c2 == m_choice2);
665  // tag options
666  QString s((f1 && f2 ? "1" : "0"));
667  if (!m_option1->conflict()) m_option1->setConflict(f1 && f2);
668  if (!m_option2->conflict()) m_option2->setConflict(f1 && f2);
669  // return value
670  return (f1 && f2);
671  }
672  return false;
673 }
674 
675 /**********************
676  * DrPageSize members *
677  **********************/
678 
679 DrPageSize::DrPageSize(const TQString& s, float width, float height, float left, float bottom, float right, float top)
680 : m_name(s),
681  m_width( width ),
682  m_height( height ),
683  m_left( left ),
684  m_bottom( bottom ),
685  m_right( right ),
686  m_top( top )
687 {
688 }
689 
690 DrPageSize::DrPageSize(const DrPageSize& d)
691 : m_name(d.m_name),
692  m_width( d.m_width ),
693  m_height( d.m_height ),
694  m_left( d.m_left ),
695  m_bottom( d.m_bottom ),
696  m_right( d.m_right ),
697  m_top( d.m_top )
698 {
699 }
700 
701 TQSize DrPageSize::pageSize() const
702 {
703  return TQSize( ( int )m_width, ( int )m_height );
704 }
705 
706 TQRect DrPageSize::pageRect() const
707 {
708  return TQRect( ( int )( m_left+0.5 ), ( int )( m_top+0.5 ), ( int )( m_width-m_left-m_right ), ( int )( m_height-m_top-m_bottom ) );
709 }
710 
711 TQSize DrPageSize::margins() const
712 {
713  return TQSize( ( int )( m_left+0.5 ), ( int )( m_top+0.5 ) );
714 }

kdeprint

Skip menu "kdeprint"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

kdeprint

Skip menu "kdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kdeprint by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |