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

tdeprint

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

tdeprint

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

tdeprint

Skip menu "tdeprint"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeprint by doxygen 1.9.4
This website is maintained by Timothy Pearson.