UltraScan III
us_editor.cpp
Go to the documentation of this file.
1 #include "us_editor.h"
2 #include "us_settings.h"
3 #include "us_gui_settings.h"
4 
5 US_Editor::US_Editor( int menu, bool readonly, const QString& extension,
6  QWidget* parent, Qt::WindowFlags flags ) : QMainWindow( parent, flags )
7 {
8  file_extension = extension;
10 
11  if ( extension.contains( "/" ) )
12  { // Extension is actually directory-code/extension
13 
14  if ( extension.startsWith( "results" ) )
16 
17  else if ( extension.startsWith( "reports" ) )
19 
20  else if ( extension.startsWith( "data" ) )
22 
23  else
24  file_directory = extension.section( "/", 0, -2 );
25 
26  file_extension = extension.section( "/", -1, -1 );
27  }
28 
29  filename = "";
30 
31 #ifndef Q_WS_MAC
32  edMenuBar = menuBar();
33 #else
34  edMenuBar = new QMenuBar( 0 );
35 #endif
36  QMenu* fileMenu = edMenuBar->addMenu( tr( "&File" ) );
37  fileMenu->setFont ( QFont( US_GuiSettings::fontFamily(),
38  US_GuiSettings::fontSize() - 1 ) );
39 
40  QAction* loadAction;
41  QAction* saveAction;
42 
43  // Add menu types as necessary
44  switch ( menu )
45  {
46  case LOAD:
47  loadAction = new QAction( tr( "&Load" ), this );;
48  loadAction->setShortcut( tr( "Ctrl+L" ) );
49  connect( loadAction, SIGNAL( triggered() ), this, SLOT( load() ) );
50 
51  saveAction = new QAction( tr( "&Save" ), this );;
52  saveAction->setShortcut( tr( "Ctrl+S" ) );
53  connect( saveAction, SIGNAL( triggered() ), this, SLOT( save() ) );
54 
55  fileMenu->addAction( loadAction );
56  fileMenu->addAction( saveAction );
57  // Fall through
58 
59  default:
60  QAction* saveAsAction = new QAction( tr( "Save&As" ), this );;
61  saveAsAction->setShortcut( tr( "Ctrl+A" ) );
62  connect( saveAsAction, SIGNAL( triggered() ), this, SLOT( saveAs() ) );
63 
64  QAction* clearAction = new QAction( tr( "Clear" ), this );;
65  connect( clearAction, SIGNAL( triggered() ), this, SLOT( clear() ) );
66 
67  QAction* printAction = new QAction( tr( "&Print" ), this );;
68  printAction->setShortcut( tr( "Ctrl+P" ) );
69  connect( printAction, SIGNAL( triggered() ), this, SLOT( print() ) );
70 
71  QAction* fontAction = new QAction( tr( "&Font" ), this );;
72  fontAction->setShortcut( tr( "Ctrl+F" ) );
73  connect( fontAction, SIGNAL( triggered() ), this, SLOT( update_font() ) );
74 
75  fileMenu->addAction( saveAsAction );
76  fileMenu->addAction( printAction );
77  fileMenu->addAction( fontAction );
78  fileMenu->addAction( clearAction );
79  }
80 
81  menuBar()->setPalette( US_GuiSettings::normalColor() );
82  menuBar()->setFont ( QFont( US_GuiSettings::fontFamily(),
84 
85  currentFont = QFont( "Courier", US_GuiSettings::fontSize() - 1,
86  QFont::Bold );
87 
88  e = new QTextEdit( this );
89  e->setFont ( currentFont );
90  e->setPalette ( US_GuiSettings::editColor() );
91  e->setAcceptRichText( true );
92  e->setWordWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
93  e->setReadOnly ( readonly );
94  setCentralWidget ( e );
95 }
96 
97 void US_Editor::load( void )
98 {
99  QString fn;
100  QString text;
101 
102  fn = QFileDialog::getOpenFileName( this,
103  tr( "Open File" ),
105  file_extension );
106 
107  if ( fn == "" ) return;
108 
109  filename = fn;
110 
111  QFile f( filename );
112 
113  if ( f.open( QIODevice::ReadOnly | QIODevice::Text ) )
114  {
115  QTextStream t( &f );
116 
117  text = t.readAll();
118  f.close( );
119 
120  e->setPlainText( text );
122  file_directory = filename.section( "/", 0, -2 );
123  }
124  else
125  QMessageBox::information( this,
126  tr( "Error" ),
127  tr( "Could not open\n\n" ) + fn + tr( "\n\n for reading." ) );
128 }
129 
131 {
132  if ( filename == "" )
133  saveAs();
134  else
135  saveFile();
136 }
137 
139 {
140  QString fn;
141 
142  fn = QFileDialog::getSaveFileName( this );
143 
144  if ( ! fn.isEmpty( ) )
145  {
146  filename = fn;
147  saveFile();
148  }
149 }
150 
152 {
153  QString text = e->toPlainText();
154 
155  QFile f( filename );
156 
157  if ( f.open( QIODevice::WriteOnly | QIODevice::Text ) )
158  {
159  QTextStream t( &f );
160 
161  t << text;
162  f.close( );
163  }
164  else
165  QMessageBox::information( this,
166  tr( "Error" ),
167  tr( "Could not open\n\n" ) + filename + tr( "\n\n for writing." ) );
168 }
169 
171 {
172  bool ok;
173  QFont newFont = QFontDialog::getFont( &ok, currentFont, this );
174 
175  if ( ok )
176  {
177  currentFont = newFont;
178  e->setFont( currentFont );
179  }
180 }
181 
182 void US_Editor::print( void )
183 {
184  QPrinter printer;
185  QPrintDialog* dialog = new QPrintDialog( &printer, this );
186 
187  dialog->setWindowTitle( tr( "Print Document" ) );
188 
189  if ( dialog->exec() != QDialog::Accepted ) return;
190 
191  QStringList text = e->toPlainText().split( "\n" );
192  QPainter p;
193 
194  p.begin( &printer ); // paint on printer
195  p.setFont( e->font() );
196 
197  int yPos = 0; // y position for each line
198  QFontMetrics fm = p.fontMetrics();
199 
200  const int MARGIN = 10;
201 
202  for ( int i = 0; i < text.size(); i++ )
203  {
204  if ( MARGIN + yPos > printer.height( ) - MARGIN )
205  {
206  printer.newPage(); // no more room on this page
207  yPos = 0; // back to top of page
208  }
209 
210  p.drawText( MARGIN, MARGIN + yPos,
211  printer.width(), fm.lineSpacing(),
212  Qt::TextExpandTabs, text[ i ] );
213 
214  yPos = yPos + fm.lineSpacing();
215  }
216 
217  p.end(); // send job to printer
218 }
219 
220