00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <kaction.h>
00025 #include <kdialogbase.h>
00026 #include <kfiledialog.h>
00027 #include <kparts/genericfactory.h>
00028 #include <kprinter.h>
00029 #include <ktempfile.h>
00030 #include "kvsprefs.h"
00031 #include <qapplication.h>
00032 #include <qpaintdevicemetrics.h>
00033 #include <qprinter.h>
00034 #include <qtooltip.h>
00035
00036 #include "ByteStream.h"
00037 #include "DjVuToPS.h"
00038 #include "kprintDialogPage_DJVUpageoptions.h"
00039 #include "kprintDialogPage_DJVUconversionoptions.h"
00040 #include "djvumultipage.h"
00041 #include "pageRangeWidget.h"
00042 #include "prefs.h"
00043
00044 #include "kmessagebox.h"
00045
00046 typedef KParts::GenericFactory<DjVuMultiPage> DjVuMultiPageFactory;
00047 K_EXPORT_COMPONENT_FACTORY(djvuviewpart, DjVuMultiPageFactory)
00048
00049
00050 DjVuMultiPage::DjVuMultiPage(QWidget *parentWidget, const char *widgetName, QObject *parent,
00051 const char *name, const QStringList&)
00052 : KMultiPage(parentWidget, widgetName, parent, name), djvuRenderer(parentWidget)
00053 {
00054
00055
00056 setInstance(DjVuMultiPageFactory::instance());
00057 djvuRenderer.setName("DjVu renderer");
00058
00059
00060 QStringList renderModes;
00061 renderModes.append(i18n("Color"));
00062 renderModes.append(i18n("Black and White"));
00063 renderModes.append(i18n("Show foreground only"));
00064 renderModes.append(i18n("Show background only"));
00065 renderModeAction = new KSelectAction (i18n("Render Mode"), 0, 0, 0, actionCollection(), "render_mode");
00066 renderModeAction->setItems(renderModes);
00067
00068 renderModeAction->setCurrentItem(Prefs::renderMode());
00069
00070 deletePagesAction = new KAction(i18n("Delete Pages..."), 0, this, SLOT(slotDeletePages()), actionCollection(), "delete_pages");
00071
00072
00073 connect(renderModeAction, SIGNAL(activated(int)), this, SLOT(setRenderMode(int)));
00074
00075
00076
00077
00078 setRenderer(&djvuRenderer);
00079
00080 setXMLFile("djvumultipage.rc");
00081
00082 enableActions(false);
00083 }
00084
00085
00086 DjVuMultiPage::~DjVuMultiPage()
00087 {
00088 ;
00089 }
00090
00091
00092 KAboutData* DjVuMultiPage::createAboutData()
00093 {
00094
00095 KAboutData* about = new KAboutData("djvumultipage", I18N_NOOP("KDjView"), "0.1",
00096 I18N_NOOP("KViewshell DjVu Plugin."),
00097 KAboutData::License_GPL,
00098 "Wilfried Huss",
00099 I18N_NOOP("This program displays DjVu files."));
00100
00101 about->addAuthor ("Stefan Kebekus",
00102 I18N_NOOP("KViewShell plugin"),
00103 "kebekus@kde.org",
00104 "http://www.mi.uni-koeln.de/~kebekus");
00105
00106 about->addAuthor ("Wilfried Huss",
00107 I18N_NOOP("DjVu file loading"),
00108 "Wilfried.Huss@gmx.at");
00109
00110 return about;
00111 }
00112
00113 void DjVuMultiPage::enableActions(bool b)
00114 {
00115 KMultiPage::enableActions(b);
00116
00117 deletePagesAction->setEnabled(b);
00118 }
00119
00120 void DjVuMultiPage::setFile(bool r)
00121 {
00122 enableActions(r);
00123 }
00124
00125 QStringList DjVuMultiPage::fileFormats() const
00126 {
00127
00128
00129 QStringList r;
00130 r << i18n("*.djvu|DjVu file (*.djvu)");
00131 return r;
00132 }
00133
00134
00135 void DjVuMultiPage::setRenderMode(int mode)
00136 {
00137
00138 switch (mode)
00139 {
00140 case Prefs::EnumRenderMode::BlackAndWhite:
00141 Prefs::setRenderMode(Prefs::EnumRenderMode::BlackAndWhite);
00142
00143 break;
00144 case Prefs::EnumRenderMode::Foreground:
00145 Prefs::setRenderMode(Prefs::EnumRenderMode::Foreground);
00146
00147 break;
00148 case Prefs::EnumRenderMode::Background:
00149 Prefs::setRenderMode(Prefs::EnumRenderMode::Background);
00150
00151 break;
00152 default:
00153 Prefs::setRenderMode(Prefs::EnumRenderMode::Color);
00154 }
00155 Prefs::writeConfig();
00156 renderModeChanged();
00157 }
00158
00159
00160 void DjVuMultiPage::slotDeletePages()
00161 {
00162 if (numberOfPages() == 0)
00163 return;
00164
00165 KDialogBase dialog( parentWdg, "urldialog", true, i18n("Delete Pages"), KDialogBase::Ok|KDialogBase::Cancel, KDialogBase::Ok, true );
00166 PageRangeWidget range( 1, numberOfPages(), currentPageNumber(), &dialog, "range widget" );
00167 QToolTip::add( &range, i18n( "Select the pages you wish to delete." ) );
00168 dialog.setButtonOK(i18n("Delete Pages"));
00169 dialog.setMainWidget(&range);
00170 if (dialog.exec() != QDialog::Accepted)
00171 return;
00172
00173 djvuRenderer.deletePages(range.getFrom(), range.getTo());
00174
00175
00176
00177 pageCache->deselectText();
00178 document_history.clear();
00179 pageCache->clear();
00180
00181 generateDocumentWidgets();
00182
00183
00184 markList()->clear();
00185 markList()->setNumberOfPages(numberOfPages(), KVSPrefs::showThumbnails());
00186
00187
00188
00189
00190
00191 emit setStatusBarText(QString::null);
00192 }
00193
00194
00195 void DjVuMultiPage::print()
00196 {
00197
00198 if (djvuRenderer.isEmpty())
00199 return;
00200
00201
00202 KPrinter *printer = getPrinter(false);
00203 if (printer == 0)
00204 return;
00205
00206 KPrintDialogPage_DJVUPageOptions *pageOptions = new KPrintDialogPage_DJVUPageOptions();
00207 if (pageOptions == 0) {
00208 kdError(1223) << "DjVuMultiPage::print(): Cannot allocate new KPrintDialogPage_PageOptions structure" << endl;
00209 delete printer;
00210 return;
00211 }
00212 printer->addDialogPage( pageOptions );
00213
00214 KPrintDialogPage_DJVUConversionOptions *conversionOptions = new KPrintDialogPage_DJVUConversionOptions();
00215 if (pageOptions == 0) {
00216 kdError(1223) << "DjVuMultiPage::print(): Cannot allocate new KPrintDialogPage_ConversionOptions structure" << endl;
00217 delete printer;
00218 return;
00219 }
00220 printer->addDialogPage( conversionOptions );
00221
00222
00223 if ( printer->setup(parentWdg, i18n("Print %1").arg(m_file.section('/', -1))) ) {
00224
00225 QValueList<int> pageList = printer->pageList();
00226 if (pageList.isEmpty())
00227 printer->abort();
00228 else {
00229
00230
00231 qApp->processEvents();
00232
00233
00234 DjVuToPS converter;
00235 DjVuToPS::Options &options = converter.options;
00236
00237
00238 options.set_format(DjVuToPS::Options::PS);
00239 QString op = printer->option( "kde-kdjvu-pslevel" );
00240 if (op == "1")
00241 options.set_level(1);
00242 else
00243 if (op == "3")
00244 options.set_level(3);
00245 else
00246 options.set_level(2);
00247
00248
00249 if (printer->option( "kde-kviewshell-rotatepage" ) == "true")
00250 options.set_orientation (DjVuToPS::Options::AUTO);
00251 else
00252 if ( printer->orientation() == KPrinter::Landscape )
00253 options.set_orientation (DjVuToPS::Options::LANDSCAPE);
00254 else
00255 options.set_orientation (DjVuToPS::Options::PORTRAIT);
00256
00257
00258 op = printer->option("kde-kdjvu-rendermode");
00259 if (op == "black-and-white")
00260 options.set_mode(DjVuToPS::Options::BW);
00261 else
00262 if (op == "foreground")
00263 options.set_mode(DjVuToPS::Options::FORE);
00264 else
00265 if (op == "background")
00266 options.set_mode(DjVuToPS::Options::BACK);
00267 else
00268 options.set_mode(DjVuToPS::Options::COLOR);
00269
00270
00271 if (printer->colorMode() == KPrinter::Color)
00272 options.set_color(true);
00273 else
00274 options.set_color(false);
00275
00276
00277 if (printer->option( "kde-kdjvu-fitpage" ) == "true")
00278 options.set_zoom(0);
00279 else
00280 options.set_zoom(100);
00281
00282 KTempFile tmpPSFile(QString::null, "ps");
00283 tmpPSFile.close();
00284 tmpPSFile.setAutoDelete(true);
00285
00286 if (djvuRenderer.convertToPSFile(converter, tmpPSFile.name(), pageList ) == true)
00287 printer->printFiles( QStringList(tmpPSFile.name()), true );
00288 else
00289 printer->abort();
00290 }
00291 delete printer;
00292 }
00293 }
00294
00295
00296 bool DjVuMultiPage::isReadWrite() const
00297 {
00298 return true;
00299 }
00300
00301
00302 bool DjVuMultiPage::isModified() const
00303 {
00304 return djvuRenderer.isModified();
00305 }
00306
00307
00308 void DjVuMultiPage::slotSave()
00309 {
00310
00311 if (djvuRenderer.isEmpty())
00312 return;
00313
00314
00315 QString formats;
00316 QString ending;
00317 int rindex = m_file.findRev(".");
00318 if (rindex == -1) {
00319 ending = QString::null;
00320 formats = QString::null;
00321 } else {
00322 ending = m_file.mid(rindex);
00323 formats = fileFormats().grep(ending).join("\n");
00324 }
00325
00326 QString fileName = KFileDialog::getSaveFileName(QString::null, formats, 0, i18n("Save File As"));
00327
00328 if (fileName.isEmpty())
00329 return;
00330
00331
00332
00333 if (!ending.isEmpty() && fileName.find(ending) == -1)
00334 fileName = fileName+ending;
00335
00336 if (QFile(fileName).exists()) {
00337 int r = KMessageBox::warningContinueCancel(parentWdg, i18n("The file %1\nalready exists. Do you want to overwrite it?").arg(fileName),
00338 i18n("Overwrite File"), i18n("Overwrite"));
00339 if (r == KMessageBox::Cancel)
00340 return;
00341 }
00342
00343 djvuRenderer.save(fileName);
00344
00345
00346
00347
00348
00349
00350
00351
00352 return;
00353 }
00354
00355
00356
00357 #include "djvumultipage.moc"