summaryrefslogtreecommitdiffstats
path: root/kcron/ktprint.cpp
blob: 56d70701512a8ded73ce99cf0018a29bcfa64cad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

/***************************************************************************
 *   --------------------------------------------------------------------  *
 *   KDE\QT printing implementation.                                       *
 *   --------------------------------------------------------------------  *
 *   Copyright (C) 1999, Robert Berry <rjmber@ntlworld.com>                *
 *   --------------------------------------------------------------------  *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include <tqpainter.h>
#include <tqstring.h>
#include <tqpaintdevicemetrics.h>

#include <ktprintopt.h>
#include <tdelocale.h>
#include "ktprint.h"

const int KTPrint::alignTextLeft             (1000);
const int KTPrint::alignTextRight            (2000);
const int KTPrint::alignTextCenter           (3000);

const int KTPrint::defaultLeftMargin         (20);
const int KTPrint::defaultRightMargin        (20);
const int KTPrint::defaultTopMargin          (30);
const int KTPrint::defaultBottomMargin       (30);

/*
  I had to add this enum to get the file to compile. Since I ported
  this class to QT2.1.1 the compiler throws errors about these flags. 
  When I figure why  I will fix this ugly solution. 
*/

enum AlignmentFlags { AlignLeft = 0x0001, AlignRight = 0x0002, 
                      AlignHCenter = 0x0004, AlignTop = 0x0008, 
                      AlignBottom = 0x0010, AlignVCenter = 0x0020,
                      AlignCenter = AlignVCenter | AlignHCenter, 
                      SingleLine = 0x0040, DontClip = 0x0080, 
                      ExpandTabs = 0x0100, ShowPrefix = 0x0200, 
                      WordBreak = 0x0400, DontPrint = 0x1000 };

KTPrint::~KTPrint()
{
  delete prnt;
  if (columns.size()>0)
    columns.erase(columns.begin(), columns.end());

}

void KTPrint :: createColumns (unsigned num_columns)
{
//Construct all of the columns to be equal in size
//I am going to add a function which works on percentages

  int start;
  Column *col;
  int col_width(width / num_columns);

  if (columns.size()>0)
    columns.erase(columns.begin(), columns.end());

  for (unsigned i = 0, start = leftMargin; i < num_columns; i++) {
    col = new Column;
    col->start = start;
    col->finish = start + (col_width - 5);
    col->height = topMargin;
    columns.push_back(col);
    start+=col_width;
  }
	
}		
	
bool KTPrint:: start ()
{
  prnt->setDocName("Cron Tab");
  prnt->addDialogPage(new KTPrintOpt(root));

  if (prnt->setup(0L, i18n("Print Cron Tab"))) { //Setup a printer
    if (paint!=NULL) delete paint;
    paint = new  TQPainter ();
    paint->begin(prnt);
    paint->setTabStops(20); // Setup a defualt tab stop size
    		
    //Get the information about the page size
    TQPaintDeviceMetrics metrics (prnt);
    width = metrics.width () - (leftMargin + rightMargin);
    height = metrics.height () - (topMargin + bottomMargin);
    return true;
  }
  else
    return false;
}	

void KTPrint :: setFont (const TQFont &font)
{
  paint->setFont(font);
}

TQFont KTPrint :: getFont () const
{
  return paint->font();	
}

void KTPrint :: print (const TQString &str, int col, int alignment, bool wordWrap)
{
//Prints the string str into the column col using
//the remaining arguments as format flags

  int format;
 
  if (paint==NULL) 
    return;
  
	
   //Setup the alignment
  switch (alignment) {
    case alignTextLeft : format = AlignLeft | AlignTop | DontClip | ExpandTabs; break;
    case alignTextCenter : format = AlignHCenter | AlignTop | DontClip | ExpandTabs; break;
    case alignTextRight : format = AlignRight | AlignTop | DontClip | ExpandTabs; break;
    default :
       //add error trap
       break;

  }
	
  //Check if we are wrapping words
  if (wordWrap)
    format = format | WordBreak;
	
  //Whats left of the page
  int remainder (height - columns[col-1]->height);
  TQRect rect=paint->boundingRect(columns[col-1]->start,columns[col-1]->height, columns[col-1]->width(), remainder,format, str);
  
  if (rect.height() <= remainder)
  {
    //Draw the text
    paint->drawText(columns[col-1]->start,columns[col-1]->height, columns[col-1]->width(), remainder, format, str);
    //Reset the columns height
    columns[col-1]->height += rect.height();
  }
  else
  {
    newPage();
    remainder = height - columns[col-1]->height;
    rect=paint->boundingRect(columns[col-1]->start,columns[col-1]->height, columns[col-1]->width(), remainder,format, str);
    paint->drawText(columns[col-1]->start,columns[col-1]->height, columns[col-1]->width(), remainder, format, str);
    columns[col-1]->height += rect.height();
  }
}

void KTPrint :: levelColumns(int space)
{
  int ht(0);
	
  //Find the heighest height
  for (unsigned i(0); i < columns.size(); i++) {
   if (ht < columns[i]->height)
     ht = columns[i]->height;
  }
	
  //Set all the columns to that height and then add the space argument
  for (unsigned i(0); i < columns.size(); i++)
    columns[i]->height = ht+space;
}

void KTPrint :: finished ()
{
  if (paint!=NULL) {
    paint->end(); //Send to printer or file
    delete paint;
  }
}

void KTPrint :: newPage ()
{
  prnt->newPage();
  for (unsigned i(0); i < columns.size(); i++)
    columns[i]->height = topMargin;
}

int KTPrint :: numCopies () const
{
  return prnt->numCopies();
}