removed version, refs #101; implemented new temporaryfile handler and improved save, refs #51; partial changes for refs #79 and solved #104
parent
b35e1fd9ed
commit
0614b4fc23
13 changed files with 1271 additions and 227 deletions
@ -0,0 +1,134 @@ |
||||
#ifndef __PDFDISPLAY_HPP__ |
||||
#define __PDFDISPLAY_HPP__ |
||||
|
||||
#include <QtCore/QFile> |
||||
#include <QtGui/QApplication> |
||||
#include <QtGui/QImage> |
||||
#include <QtGui/QLabel> |
||||
#include <QtGui/QMouseEvent> |
||||
#include <QtGui/QPainter> |
||||
#include <QtGui/QPaintEvent> |
||||
#include <QtGui/QToolTip> |
||||
#include <QtGui/QWidget> |
||||
|
||||
#include <QtCore/QDebug> |
||||
|
||||
#include <poppler/qt4/poppler-qt4.h> |
||||
|
||||
class PDFDisplay : public QWidget // picture display widget
|
||||
{ |
||||
public: |
||||
PDFDisplay( Poppler::Document *d, bool arthur ) |
||||
{ |
||||
showTextRects = false; |
||||
doc = d; |
||||
m_currentPage = 0; |
||||
if (arthur) |
||||
{ |
||||
backendString = "Arthur"; |
||||
doc->setRenderBackend(Poppler::Document::ArthurBackend); |
||||
} |
||||
else |
||||
{ |
||||
backendString = "Splash"; |
||||
doc->setRenderBackend(Poppler::Document::SplashBackend); |
||||
} |
||||
doc->setRenderHint(Poppler::Document::Antialiasing, true); |
||||
doc->setRenderHint(Poppler::Document::TextAntialiasing, true); |
||||
} |
||||
~PDFDisplay() |
||||
{ |
||||
qDeleteAll(textRects); |
||||
delete doc; |
||||
} |
||||
|
||||
void setShowTextRects(bool show) |
||||
{ |
||||
showTextRects = show; |
||||
} |
||||
void display() |
||||
{ |
||||
if (doc) { |
||||
Poppler::Page *page = doc->page(m_currentPage); |
||||
if (page) { |
||||
qDebug() << "Displaying page using" << backendString << "backend: " << m_currentPage; |
||||
QTime t = QTime::currentTime(); |
||||
image = page->renderToImage(); |
||||
qDebug() << "Rendering took" << t.msecsTo(QTime::currentTime()) << "msecs"; |
||||
qDeleteAll(textRects); |
||||
if (showTextRects) |
||||
{ |
||||
QPainter painter(&image); |
||||
painter.setPen(Qt::red); |
||||
textRects = page->textList(); |
||||
for (auto tb(textRects.begin()); tb!=textRects.end(); ++tb) |
||||
painter.drawRect((*tb)->boundingBox()); |
||||
} |
||||
else textRects.clear(); |
||||
update(); |
||||
delete page; |
||||
} |
||||
} else { |
||||
qWarning() << "doc not loaded"; |
||||
} |
||||
} |
||||
|
||||
protected: |
||||
void paintEvent( QPaintEvent * ) |
||||
{ |
||||
QPainter paint( this ); // paint widget
|
||||
if (!image.isNull()) { |
||||
paint.drawImage(0, 0, image); |
||||
} else { |
||||
qWarning() << "null image"; |
||||
} |
||||
} |
||||
void keyPressEvent( QKeyEvent *e ) |
||||
{ |
||||
if (e->key() == Qt::Key_Down) |
||||
{ |
||||
if (m_currentPage + 1 < doc->numPages()) |
||||
{ |
||||
m_currentPage++; |
||||
display(); |
||||
} |
||||
} |
||||
else if (e->key() == Qt::Key_Up) |
||||
{ |
||||
if (m_currentPage > 0) |
||||
{ |
||||
m_currentPage--; |
||||
display(); |
||||
} |
||||
} |
||||
else if (e->key() == Qt::Key_Q) |
||||
{ |
||||
exit(0); |
||||
} |
||||
} |
||||
void mousePressEvent( QMouseEvent *e ) |
||||
{ |
||||
int i = 0; |
||||
for (auto tb(textRects.begin()); tb!=textRects.end(); ++tb) |
||||
{ |
||||
if ((*tb)->boundingBox().contains(e->pos())) |
||||
{ |
||||
QString tt = QString("Text: \"%1\"\nIndex in text list: %2").arg((*tb)->text()).arg(i); |
||||
QToolTip::showText(e->globalPos(), tt, this); |
||||
break; |
||||
} |
||||
++i; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
int m_currentPage; |
||||
QImage image; |
||||
Poppler::Document *doc; |
||||
QString backendString; |
||||
bool showTextRects; |
||||
QList<Poppler::TextBox*> textRects; |
||||
}; |
||||
|
||||
|
||||
#endif |
@ -0,0 +1,122 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef __PLUGINFACTORY_HXX__ |
||||
#define __PLUGINFACTORY_HXX__ |
||||
|
||||
#include <QtWebKit/QWebPluginFactory> |
||||
#include <poppler/qt4/poppler-qt4.h> |
||||
#include <pdfdisplay.hpp> |
||||
|
||||
#include <QtCore/QDebug> |
||||
#ifndef LOG |
||||
#define LOG qDebug()<<__PRETTY_FUNCTION__ |
||||
#endif |
||||
|
||||
#define foreach(A, B) \ |
||||
auto foreach_loopvar_##A(B); \
|
||||
for (auto A(foreach_loopvar_##A.begin()); A!=foreach_loopvar_##A.end(); ++A) |
||||
|
||||
class PluginFactory: public QWebPluginFactory { |
||||
public: |
||||
PluginFactory(QObject* p=0): QWebPluginFactory(p) { |
||||
LOG; |
||||
Plugin plugin; |
||||
plugin.name = "Show PDF-Document"; |
||||
plugin.description = "Plugin for PDF documents"; |
||||
MimeType mime; |
||||
mime.fileExtensions<<"pdf"; |
||||
mime.name = "application/pdf"; |
||||
mime.description = "PDF-Document"; |
||||
plugin.mimeTypes<<mime; |
||||
_plugins.push_back(plugin); |
||||
} |
||||
virtual QObject* create(const QString& mimeType, const QUrl& url, |
||||
const QStringList& argumentNames, |
||||
const QStringList& argumentValues ) const { |
||||
LOG<<"mimeType:"<<mimeType |
||||
<<"url:"<<url |
||||
<<"argumentNames:"<<argumentNames.join(", ") |
||||
<<"argumentValues:"<<argumentValues.join(", "); |
||||
if (mimeType=="application/pdf") { |
||||
Poppler::Document *doc = Poppler::Document::load(url.path()); |
||||
assert(doc); |
||||
assert(!doc->isLocked()); |
||||
|
||||
// output some meta-data
|
||||
int major = 0, minor = 0; |
||||
doc->getPdfVersion( &major, &minor ); |
||||
qDebug() << " PDF Version: " << qPrintable(QString::fromLatin1("%1.%2").arg(major).arg(minor)); |
||||
qDebug() << " Title: " << doc->info("Title"); |
||||
qDebug() << " Subject: " << doc->info("Subject"); |
||||
qDebug() << " Author: " << doc->info("Author"); |
||||
qDebug() << " Key words: " << doc->info("Keywords"); |
||||
qDebug() << " Creator: " << doc->info("Creator"); |
||||
qDebug() << " Producer: " << doc->info("Producer"); |
||||
qDebug() << " Date created: " << doc->date("CreationDate").toString(); |
||||
qDebug() << " Date modified: " << doc->date("ModDate").toString(); |
||||
qDebug() << "Number of pages: " << doc->numPages(); |
||||
qDebug() << " Linearised: " << doc->isLinearized(); |
||||
qDebug() << " Encrypted: " << doc->isEncrypted(); |
||||
qDebug() << " OK to print: " << doc->okToPrint(); |
||||
qDebug() << " OK to copy: " << doc->okToCopy(); |
||||
qDebug() << " OK to change: " << doc->okToChange(); |
||||
qDebug() << "OK to add notes: " << doc->okToAddNotes(); |
||||
qDebug() << " Page mode: " << doc->pageMode(); |
||||
qDebug() << " Metadata: " << doc->metadata(); |
||||
QStringList fontNameList; |
||||
foreach(font, doc->fonts()) |
||||
fontNameList += font->name(); |
||||
qDebug() << " Fonts: " << fontNameList.join( ", " ); |
||||
|
||||
if ( doc->hasEmbeddedFiles() ) { |
||||
qDebug() << "Embedded files:"; |
||||
foreach(file, doc->embeddedFiles()) { |
||||
qDebug() << " " << (*file)->name(); |
||||
} |
||||
qDebug(); |
||||
} else { |
||||
qDebug() << "No embedded files"; |
||||
} |
||||
|
||||
if (doc->numPages() <= 0) |
||||
{ |
||||
delete doc; |
||||
qDebug() << "Doc has no pages"; |
||||
return 0; |
||||
} |
||||
|
||||
{ |
||||
std::auto_ptr<Poppler::Page> page(doc->page(0)); |
||||
qDebug() << "Page 1 size: " << page->pageSize().width()/72 << "inches x " << page->pageSize().height()/72 << "inches"; |
||||
} |
||||
|
||||
bool useArthur(false); |
||||
PDFDisplay* test(new PDFDisplay(doc, useArthur)); |
||||
test->setWindowTitle("Poppler-Qt4 Test"); |
||||
test->setShowTextRects(false); |
||||
test->display(); |
||||
test->show(); |
||||
// QMainWindow* main(new QMainWindow);
|
||||
// main->setCentralWidget(test);
|
||||
// main->show();
|
||||
return test; |
||||
} |
||||
return 0; |
||||
} |
||||
virtual QList<Plugin> plugins() const { |
||||
LOG; |
||||
return _plugins; |
||||
} |
||||
virtual void refreshPlugins() { |
||||
LOG; |
||||
} |
||||
private: |
||||
QList<Plugin> _plugins; |
||||
}; |
||||
|
||||
#endif |
@ -0,0 +1,121 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef __SAVEORRUN_HXX__ |
||||
#define __SAVEORRUN_HXX__ |
||||
|
||||
#include <ui_saveorrun.h> |
||||
|
||||
#include <QtGui/QDialog> |
||||
#include <QtGui/QFileDialog> |
||||
#include <QtGui/QFileSystemModel> |
||||
#include <QtGui/QMessageBox> |
||||
#include <memory> |
||||
|
||||
class SaveOrRun: public QDialog, protected Ui::SaveOrRun { |
||||
Q_OBJECT; |
||||
public: |
||||
|
||||
SaveOrRun(QString obj, QString type, QString src, |
||||
bool kiosk=false, QWidget* p=0): |
||||
QDialog(p), _ok(false) { |
||||
setupUi(this); |
||||
_object->setText(obj); |
||||
_type->setText(type); |
||||
_source->setText(src); |
||||
QString path(QDir::homePath()); |
||||
QStringList defpaths; |
||||
defpaths<<"downloads"<<"Downloads"<<"Documents" |
||||
<<tr("Dokumente", "Documents folder in local language") |
||||
<<"Desktop" |
||||
<<tr("Arbeitsfläche", "Desktop folder in local language"); |
||||
for (auto it(defpaths.begin()); it!=defpaths.end(); ++it) |
||||
if (QFile::exists(QDir::homePath()+QDir::separator()+*it)) { |
||||
path = QDir::homePath()+QDir::separator()+*it; |
||||
break; |
||||
} |
||||
_filename->setText(path+QDir::separator()+obj); |
||||
_program->setText(QCoreApplication::applicationDirPath() |
||||
+QDir::separator()); |
||||
// if (kiosk) {
|
||||
// _openWith->hide();
|
||||
// _program->hide();
|
||||
// _programpath->hide();
|
||||
// _saveAs->hide();
|
||||
// _filenamelabel->setText(_saveAs->text()+' '+_filenamelabel->text());
|
||||
// }
|
||||
} |
||||
|
||||
void init() { |
||||
_ok = false; |
||||
} |
||||
|
||||
bool save() { |
||||
return _saveAs->isChecked(); |
||||
} |
||||
|
||||
bool run() { |
||||
return _openWith->isChecked(); |
||||
} |
||||
|
||||
bool ok() { |
||||
return _ok; |
||||
} |
||||
|
||||
QString filename() { |
||||
return _filename->text(); |
||||
} |
||||
|
||||
QString program() { |
||||
return _program->text(); |
||||
} |
||||
|
||||
public Q_SLOTS: |
||||
|
||||
virtual void accept() { |
||||
if (save() && QFileInfo(filename()).exists() |
||||
&& QMessageBox::question(this, tr("File Exists"), |
||||
tr("File already exists:\n\n" |
||||
"%1\n\n" |
||||
"Overwrite?").arg(filename()), |
||||
QMessageBox::Yes|QMessageBox::No) |
||||
== QMessageBox::No |
||||
|| run() && (!QFile::exists(program()) |
||||
|| !QFileInfo(program()).isExecutable()) |
||||
|| save() && filename().size()==0 |
||||
|| run() && program().size()==0) |
||||
return; // reject
|
||||
_ok = true; |
||||
QDialog::accept(); |
||||
} |
||||
|
||||
protected Q_SLOTS: |
||||
|
||||
void on__browseSaveAs_clicked(bool) { |
||||
QString saveFile |
||||
(QFileDialog::getSaveFileName(this, tr("Save File As ..."), |
||||
_filename->text(), QString(), 0, |
||||
QFileDialog::DontConfirmOverwrite)); |
||||
if (!saveFile.size()) return; |
||||
if (QFileInfo(saveFile).isDir()) |
||||
saveFile += QDir::separator()+_object->text(); |
||||
_filename->setText(saveFile); |
||||
} |
||||
|
||||
void on__browseOpenWith_clicked(bool) { |
||||
QString openFile |
||||
(QFileDialog::getOpenFileName(this, tr("Open File With ..."), |
||||
_program->text())); |
||||
if (openFile.size()) _program->setText(openFile); |
||||
} |
||||
|
||||
private: |
||||
|
||||
bool _ok; |
||||
}; |
||||
|
||||
#endif |
@ -0,0 +1,284 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<ui version="4.0"> |
||||
<class>SaveOrRun</class> |
||||
<widget class="QDialog" name="SaveOrRun"> |
||||
<property name="geometry"> |
||||
<rect> |
||||
<x>0</x> |
||||
<y>0</y> |
||||
<width>583</width> |
||||
<height>161</height> |
||||
</rect> |
||||
</property> |
||||
<property name="windowTitle"> |
||||
<string>File Downloaded</string> |
||||
</property> |
||||
<layout class="QGridLayout" name="gridLayout"> |
||||
<item row="0" column="0"> |
||||
<widget class="QLabel" name="label_2"> |
||||
<property name="font"> |
||||
<font> |
||||
<weight>75</weight> |
||||
<bold>true</bold> |
||||
</font> |
||||
</property> |
||||
<property name="text"> |
||||
<string>File:</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="0" column="1" colspan="3"> |
||||
<widget class="QLabel" name="_object"> |
||||
<property name="sizePolicy"> |
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> |
||||
<horstretch>0</horstretch> |
||||
<verstretch>0</verstretch> |
||||
</sizepolicy> |
||||
</property> |
||||
<property name="text"> |
||||
<string>...</string> |
||||
</property> |
||||
<property name="textInteractionFlags"> |
||||
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="1" column="0"> |
||||
<widget class="QLabel" name="label_3"> |
||||
<property name="font"> |
||||
<font> |
||||
<weight>75</weight> |
||||
<bold>true</bold> |
||||
</font> |
||||
</property> |
||||
<property name="text"> |
||||
<string>Type:</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="1" column="1" colspan="3"> |
||||
<widget class="QLabel" name="_type"> |
||||
<property name="sizePolicy"> |
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> |
||||
<horstretch>0</horstretch> |
||||
<verstretch>0</verstretch> |
||||
</sizepolicy> |
||||
</property> |
||||
<property name="text"> |
||||
<string>...</string> |
||||
</property> |
||||
<property name="textInteractionFlags"> |
||||
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="2" column="0"> |
||||
<widget class="QLabel" name="label_4"> |
||||
<property name="font"> |
||||
<font> |
||||
<weight>75</weight> |
||||
<bold>true</bold> |
||||
</font> |
||||
</property> |
||||
<property name="text"> |
||||
<string>Source</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="2" column="1" colspan="3"> |
||||
<widget class="QLabel" name="_source"> |
||||
<property name="sizePolicy"> |
||||
<sizepolicy hsizetype="Preferred" vsizetype="Preferred"> |
||||
<horstretch>0</horstretch> |
||||
<verstretch>0</verstretch> |
||||
</sizepolicy> |
||||
</property> |
||||
<property name="text"> |
||||
<string>...</string> |
||||
</property> |
||||
<property name="textInteractionFlags"> |
||||
<set>Qt::LinksAccessibleByMouse|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="3" column="0"> |
||||
<widget class="QRadioButton" name="_openWith"> |
||||
<property name="text"> |
||||
<string>open file with:</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="3" column="1" colspan="2"> |
||||
<widget class="QLineEdit" name="_program"> |
||||
<property name="enabled"> |
||||
<bool>false</bool> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="3" column="3"> |
||||
<widget class="QPushButton" name="_browseOpenWith"> |
||||
<property name="enabled"> |
||||
<bool>false</bool> |
||||
</property> |
||||
<property name="sizePolicy"> |
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed"> |
||||
<horstretch>0</horstretch> |
||||
<verstretch>0</verstretch> |
||||
</sizepolicy> |
||||
</property> |
||||
<property name="text"> |
||||
<string>browse ...</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="4" column="0"> |
||||
<widget class="QRadioButton" name="_saveAs"> |
||||
<property name="text"> |
||||
<string>save file as</string> |
||||
</property> |
||||
<property name="checked"> |
||||
<bool>true</bool> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="4" column="1" colspan="2"> |
||||
<widget class="QLineEdit" name="_filename"/> |
||||
</item> |
||||
<item row="4" column="3"> |
||||
<widget class="QPushButton" name="_browseSaveAs"> |
||||
<property name="sizePolicy"> |
||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed"> |
||||
<horstretch>0</horstretch> |
||||
<verstretch>0</verstretch> |
||||
</sizepolicy> |
||||
</property> |
||||
<property name="text"> |
||||
<string>browse ...</string> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="6" column="0" colspan="4"> |
||||
<widget class="QDialogButtonBox" name="buttonBox"> |
||||
<property name="orientation"> |
||||
<enum>Qt::Horizontal</enum> |
||||
</property> |
||||
<property name="standardButtons"> |
||||
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> |
||||
</property> |
||||
</widget> |
||||
</item> |
||||
<item row="5" column="0" colspan="4"> |
||||
<spacer name="verticalSpacer"> |
||||
<property name="orientation"> |
||||
<enum>Qt::Vertical</enum> |
||||
</property> |
||||
<property name="sizeHint" stdset="0"> |
||||
<size> |
||||
<width>20</width> |
||||
<height>40</height> |
||||
</size> |
||||
</property> |
||||
</spacer> |
||||
</item> |
||||
</layout> |
||||
</widget> |
||||
<resources/> |
||||
<connections> |
||||
<connection> |
||||
<sender>buttonBox</sender> |
||||
<signal>accepted()</signal> |
||||
<receiver>SaveOrRun</receiver> |
||||
<slot>accept()</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>248</x> |
||||
<y>254</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>157</x> |
||||
<y>274</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>buttonBox</sender> |
||||
<signal>rejected()</signal> |
||||
<receiver>SaveOrRun</receiver> |
||||
<slot>reject()</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>316</x> |
||||
<y>260</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>286</x> |
||||
<y>274</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>_openWith</sender> |
||||
<signal>toggled(bool)</signal> |
||||
<receiver>_program</receiver> |
||||
<slot>setEnabled(bool)</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>105</x> |
||||
<y>170</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>232</x> |
||||
<y>172</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>_saveAs</sender> |
||||
<signal>toggled(bool)</signal> |
||||
<receiver>_filename</receiver> |
||||
<slot>setEnabled(bool)</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>92</x> |
||||
<y>463</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>220</x> |
||||
<y>463</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>_openWith</sender> |
||||
<signal>toggled(bool)</signal> |
||||
<receiver>_browseOpenWith</receiver> |
||||
<slot>setEnabled(bool)</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>82</x> |
||||
<y>79</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>531</x> |
||||
<y>79</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
<connection> |
||||
<sender>_saveAs</sender> |
||||
<signal>toggled(bool)</signal> |
||||
<receiver>_browseSaveAs</receiver> |
||||
<slot>setEnabled(bool)</slot> |
||||
<hints> |
||||
<hint type="sourcelabel"> |
||||
<x>82</x> |
||||
<y>109</y> |
||||
</hint> |
||||
<hint type="destinationlabel"> |
||||
<x>531</x> |
||||
<y>109</y> |
||||
</hint> |
||||
</hints> |
||||
</connection> |
||||
</connections> |
||||
</ui> |
@ -0,0 +1,66 @@ |
||||
/*! @file
|
||||
|
||||
@id $Id$ |
||||
*/ |
||||
// 1 2 3 4 5 6 7 8
|
||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
|
||||
|
||||
#ifndef __TEMPORARYFILE_HXX__ |
||||
#define __TEMPORARYFILE_HXX__ |
||||
|
||||
#include <QtCore/QTemporaryFile> |
||||
#include <QtCore/QRegExp> |
||||
|
||||
#include <QtCore/QDebug> |
||||
|
||||
class TemporaryFile: public QFile { |
||||
Q_OBJECT; |
||||
|
||||
public: |
||||
|
||||
TemporaryFile(QString filename): _autoRemove(false) { |
||||
if (exists(filename)) { |
||||
QString prefix(filename); |
||||
QString suffix(filename); |
||||
prefix.remove(QRegExp("\\.[^.]*$")); |
||||
suffix.remove(QRegExp("^.*\\.")); |
||||
qDebug()<<"filename"<<filename |
||||
<<"prefix"<<prefix |
||||
<<"suffix"<<suffix; |
||||
if (prefix==suffix) suffix.clear(); |
||||
for (unsigned long i(0); i<10000; ++i) |
||||
if (!exists(filename = |
||||
QString("%1_%2.%3") |
||||
.arg(prefix) |
||||
.arg(i, 4, 10, QChar('0')) |
||||
.arg(suffix))) break; |
||||
} |
||||
qDebug()<<"filename"<<filename; |
||||
setFileName(filename); |
||||
} |
||||
|
||||
bool open() { |
||||
return QFile::open(QIODevice::ReadWrite); |
||||
} |
||||
|
||||
void setAutoRemove(bool flag) { |
||||
_autoRemove = flag; |
||||
} |
||||
|
||||
~TemporaryFile() { |
||||
qDebug()<<"_autoRemove"<<_autoRemove |
||||
<<"exists()"<<exists(); |
||||
if (_autoRemove && exists()) { |
||||
qDebug()<<"Remove: "<<fileName(); |
||||
remove(); |
||||
} else { |
||||
qDebug()<<"Don't remove: "<<fileName(); |
||||
} |
||||
} |
||||
|
||||
private: |
||||
|
||||
bool _autoRemove; |
||||
}; |
||||
|
||||
#endif |
Loading…
Reference in new issue