summaryrefslogtreecommitdiffstats
path: root/kviewshell/plugins/djvu/djvumultipage.cpp
blob: 0f0c7074f4ec916ec2584919d6df6e6684a42978 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***************************************************************************
 *   Copyright (C) 2005 by Stefan Kebekus                                  *
 *   kebekus@kde.org                                                       *
 *                                                                         *
 *   Copyright (C) 2005 by Wilfried Huss                                   *
 *   Wilfried.Huss@gmx.at                                                  *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include <kaction.h>
#include <kdialogbase.h>
#include <kfiledialog.h>
#include <kparts/genericfactory.h>
#include <kprinter.h>
#include <ktempfile.h>
#include "kvsprefs.h"
#include <tqapplication.h>
#include <tqpaintdevicemetrics.h>
#include <tqprinter.h>
#include <tqtooltip.h>

#include "ByteStream.h"
#include "DjVuToPS.h"
#include "kprintDialogPage_DJVUpageoptions.h"
#include "kprintDialogPage_DJVUconversionoptions.h"
#include "djvumultipage.h"
#include "pageRangeWidget.h"
#include "prefs.h"

#include "kmessagebox.h"

typedef KParts::GenericFactory<DjVuMultiPage> DjVuMultiPageFactory;
K_EXPORT_COMPONENT_FACTORY(djvuviewpart, DjVuMultiPageFactory)


DjVuMultiPage::DjVuMultiPage(TQWidget *parentWidget, const char *widgetName, TQObject *parent,
                             const char *name, const TQStringList&)
  : KMultiPage(parentWidget, widgetName, parent, name), djvuRenderer(parentWidget)
{
  /* This is kparts wizardry that cannot be understood by man. Simply
     change the names to match your implementation.  */
  setInstance(DjVuMultiPageFactory::instance());
  djvuRenderer.setName("DjVu renderer");

  // Render modes
  TQStringList renderModes;
  renderModes.append(i18n("Color"));
  renderModes.append(i18n("Black and White"));
  renderModes.append(i18n("Show foreground only"));
  renderModes.append(i18n("Show background only"));
  renderModeAction = new KSelectAction (i18n("Render Mode"), 0, 0, 0, actionCollection(), "render_mode");
  renderModeAction->setItems(renderModes);

  renderModeAction->setCurrentItem(Prefs::renderMode());

  deletePagesAction = new KAction(i18n("Delete Pages..."), 0, this, TQT_SLOT(slotDeletePages()), actionCollection(), "delete_pages");

  // change the rendermode
  connect(renderModeAction, TQT_SIGNAL(activated(int)), this, TQT_SLOT(setRenderMode(int)));

  /* It is very important that this method is called in the
     constructor. Otherwise kmultipage does not know how to render
     files, and crashes may result. */
  setRenderer(&djvuRenderer);

  setXMLFile("djvumultipage.rc");

  enableActions(false);
}


DjVuMultiPage::~DjVuMultiPage()
{
  ;
}


KAboutData* DjVuMultiPage::createAboutData()
{
  /* You obviously want to change this to match your setup */
  KAboutData* about = new KAboutData("djvumultipage", I18N_NOOP("KDjView"), "0.1",
                          I18N_NOOP("KViewshell DjVu Plugin."),
                          KAboutData::License_GPL,
                          "Wilfried Huss",
                          I18N_NOOP("This program displays DjVu files."));

  about->addAuthor ("Stefan Kebekus",
                    I18N_NOOP("KViewShell plugin"),
                    "kebekus@kde.org",
                    "http://www.mi.uni-koeln.de/~kebekus");

  about->addAuthor ("Wilfried Huss",
                    I18N_NOOP("DjVu file loading"),
                    "Wilfried.Huss@gmx.at");

  return about;
}

void DjVuMultiPage::enableActions(bool b)
{
  KMultiPage::enableActions(b);

  deletePagesAction->setEnabled(b);
}

void DjVuMultiPage::setFile(bool r)
{
  enableActions(r);
}

TQStringList DjVuMultiPage::fileFormats() const
{
  /* This list is used in the file selection dialog when the file is
     saved */
  TQStringList r;
  r << i18n("*.djvu|DjVu file (*.djvu)");
  return r;
}


void DjVuMultiPage::setRenderMode(int mode)
{
  // Save renderMode for future uses
  switch (mode)
  {
    case Prefs::EnumRenderMode::BlackAndWhite:
      Prefs::setRenderMode(Prefs::EnumRenderMode::BlackAndWhite);

      break;
    case Prefs::EnumRenderMode::Foreground:
      Prefs::setRenderMode(Prefs::EnumRenderMode::Foreground);

      break;
    case Prefs::EnumRenderMode::Background:
      Prefs::setRenderMode(Prefs::EnumRenderMode::Background);

      break;
    default:  //Prefs::EnumRenderMode::Color
      Prefs::setRenderMode(Prefs::EnumRenderMode::Color);
  }
  Prefs::writeConfig();
  renderModeChanged();
}


void DjVuMultiPage::slotDeletePages()
{
  if (numberOfPages() == 0)
    return;
  
  KDialogBase dialog( parentWdg, "urldialog", true, i18n("Delete Pages"), KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true );
  PageRangeWidget range( 1, numberOfPages(), currentPageNumber(), &dialog, "range widget" );
  TQToolTip::add( &range, i18n( "Select the pages you wish to delete." ) );
  dialog.setButtonOK(i18n("Delete Pages"));
  dialog.setMainWidget(&range);
  if (dialog.exec() != TQDialog::Accepted)
    return;

  djvuRenderer.deletePages(range.getFrom(), range.getTo());


  // =========
  pageCache->deselectText();
  document_history.clear();
  pageCache->clear();
  
  generateDocumentWidgets();
  
  // Set number of widgets in the thumbnail sidebar
  markList()->clear();
  markList()->setNumberOfPages(numberOfPages(), KVSPrefs::showThumbnails());
  
  // Set Table of Contents
  //@@@@@@@@@@  tableOfContents->setContents(renderer->getBookmarks());
  
  // Clear Statusbar
  emit setStatusBarText(TQString::null);
}


void DjVuMultiPage::print()
{
  // Paranoid safety checks
  if (djvuRenderer.isEmpty())
    return;

  // Allocate the printer structure
  KPrinter *printer = getPrinter(false);
  if (printer == 0)
    return;

  KPrintDialogPage_DJVUPageOptions *pageOptions = new KPrintDialogPage_DJVUPageOptions();
  if (pageOptions == 0) {
    kdError(1223) << "DjVuMultiPage::print(): Cannot allocate new KPrintDialogPage_PageOptions structure" << endl;
    delete printer;
    return;
  }
  printer->addDialogPage( pageOptions );

  KPrintDialogPage_DJVUConversionOptions *conversionOptions = new KPrintDialogPage_DJVUConversionOptions();
  if (pageOptions == 0) {
    kdError(1223) << "DjVuMultiPage::print(): Cannot allocate new KPrintDialogPage_ConversionOptions structure" << endl;
    delete printer;
    return;
  }
  printer->addDialogPage( conversionOptions );
  
  // initialize the printer using the print dialog
  if ( printer->setup(parentWdg, i18n("Print %1").arg(m_file.section('/', -1))) ) {    
    // Now do the printing. 
    TQValueList<int> pageList = printer->pageList();
    if (pageList.isEmpty()) 
      printer->abort();
    else {
      // Printing usually takes a while. This is to keep the GUI
      // updated.
      qApp->processEvents();
      
      // Printing...
      DjVuToPS converter;
      DjVuToPS::Options &options = converter.options;
      
      // Set PostScript Language Level, taking 3 as the default
      options.set_format(DjVuToPS::Options::PS);
      TQString op = printer->option( "kde-kdjvu-pslevel" );
      if (op == "1")
	options.set_level(1);
      else
	if (op == "3")
	  options.set_level(3);
	else
	  options.set_level(2);
      
      // Set page size orientation
      if (printer->option( "kde-kviewshell-rotatepage" ) == "true")
	options.set_orientation (DjVuToPS::Options::AUTO);
      else
	if ( printer->orientation() == KPrinter::Landscape )
	  options.set_orientation (DjVuToPS::Options::LANDSCAPE);
	else
	  options.set_orientation (DjVuToPS::Options::PORTRAIT);
      
      // Set render mode, taking "color" as default
      op = printer->option("kde-kdjvu-rendermode");
      if (op == "black-and-white")
	options.set_mode(DjVuToPS::Options::BW);
      else
	if (op == "foreground")
	  options.set_mode(DjVuToPS::Options::FORE);
	else
	  if (op == "background")
	    options.set_mode(DjVuToPS::Options::BACK);
	  else
	    options.set_mode(DjVuToPS::Options::COLOR);
      
      // Set Color or Grayscale mode
      if (printer->colorMode() == KPrinter::Color)
	options.set_color(true);
      else
	options.set_color(false);
      
      // Set Zoom
      if (printer->option( "kde-kdjvu-fitpage" ) == "true")
	options.set_zoom(0);
      else
	options.set_zoom(100);
      
      KTempFile tmpPSFile(TQString::null, "ps");
      tmpPSFile.close();
      tmpPSFile.setAutoDelete(true);
      
      if (djvuRenderer.convertToPSFile(converter, tmpPSFile.name(), pageList ) == true)
	printer->printFiles( TQStringList(tmpPSFile.name()), true );
      else
	printer->abort();
    }
    delete printer;
  }
}


bool DjVuMultiPage::isReadWrite() const
{
  return true;
}


bool DjVuMultiPage::isModified() const
{
  return djvuRenderer.isModified();
}


void DjVuMultiPage::slotSave()
{
  // Paranoid safety checks
  if (djvuRenderer.isEmpty())
    return;

  // Try to guess the proper ending...
  TQString formats;
  TQString ending;
  int rindex = m_file.findRev(".");
  if (rindex == -1) {
    ending = TQString::null;
    formats = TQString::null;
  } else {
    ending = m_file.mid(rindex); // e.g. ".dvi"
    formats = fileFormats().grep(ending).join("\n");
  }

  TQString fileName = KFileDialog::getSaveFileName(TQString::null, formats, 0, i18n("Save File As"));

  if (fileName.isEmpty())
    return;

  // Add the ending to the filename. I hope the user likes it that
  // way.
  if (!ending.isEmpty() && fileName.find(ending) == -1)
    fileName = fileName+ending;

  if (TQFile(fileName).exists()) {
    int r = KMessageBox::warningContinueCancel(parentWdg, i18n("The file %1\nalready exists. Do you want to overwrite it?").arg(fileName),
					       i18n("Overwrite File"), i18n("Overwrite"));
    if (r == KMessageBox::Cancel)
      return;
  }
  
  djvuRenderer.save(fileName);

  /*
  if (!djvuRenderer.save(fileName) == false)
    KMessageBox::error( parentWdg,
			i18n("<qt><strong>File error.</strong> Unable to write to the specified file '%1'. The document is <strong>not</strong> saved.</qt>").arg(fileName),
			i18n("File Error"));
  */
  
  return;
}



#include "djvumultipage.moc"