#ifndef SPMFILELOADDIALOG_H #define SPMFILELOADDIALOG_H #include <QFileDialog> #include <QCheckBox> #include <QTextEdit> #include <QPushButton> #include <qfiledialog.h> #include <QFile> class spmFileLoadDialog : public QFileDialog { Q_OBJECT public: explicit spmFileLoadDialog(QObject *parent = 0); ~spmFileLoadDialog(); protected: virtual void customEvent(QEvent *e); virtual void done(int result); virtual void accept(); virtual void changeEvent(QEvent *e); virtual bool eventFilter(QObject *obj, QEvent *event); virtual void resizeEvent(QResizeEvent *event); virtual void showEvent(QShowEvent *); signals: public slots: void newCall(); void saveCall(); void saveAsCall(); void loadCall(); void deleteCall(); void fileSelectedChanged(const QString &file); void setBrowseDirectory(const QString &directory) { curdirectory = directory; this->setDirectory(directory); }; void setSaveLoadMode(int mode = 0) { if(mode == 1) { setLabelText(QFileDialog::Accept, "Save"); } }; int showRenameDialog(); void hideRenameDialog(); void renameFile(QString fpath, QString fname); void copyFile(QString fpath, QString fname); private: QString caption; QString curdirectory; QString filter; QCheckBox *checkBox; QTextEdit *pTextEditor; QPushButton *pLoadBtn, *pSaveBtn, *pSaveAsBtn, *pNewBtn, *pDelBtn; QString currFile; QString currLoadedFile; bool m_bModified; }; #endif // SPMFILELOADDIALOG_H |
#include "spmFileLoadDialog.h" #include <stdio.h> #include <stdlib.h> #include <superlog.h> #include <QMessageBox> #include <HResourceRO.h> #include <QPalette> #include <QLabel> #include <QKeyEvent> #include <QCoreApplication> #include <QFile> #include <QVBoxLayout> static QDialog *pRenameDialog; //static QTextEdit *pNameText; static QLabel *pNameLabel; spmFileLoadDialog::spmFileLoadDialog(QObject *parent) :QFileDialog((QWidget *)parent, tr("Open File"), tr("/home/lcdrm/recipe"), tr("Image Files (*.rcp)") ) { /* //this->setGeometry(this->geometry().x(), this->geometry().y(), 800, this->geometry().height()); checkBox = new QCheckBox(this); checkBox->setText("Include Original Document"); checkBox->setCheckState(Qt::Checked); //QFileDialogPrivate *pprivate; = reinterpret_cast<d_ptr>; //pprivate->qFileDialogUi->gridLayout->addWidget(checkBox,4,1); //setAcceptMode(QFileDialog::AcceptOpen); //setFileMode(QFileDialog::ExistingFile); //checkBox->setGeometry(); checkBox->show(); */ pRenameDialog = new QDialog(this); pRenameDialog->setModal(TRUE); pNameLabel = new QLabel(pRenameDialog); pNameLabel->setGeometry(10, 10, 130, 20); pNameLabel->setText("FileName:"); pNameText = new QTextEdit(pRenameDialog); pNameText->setGeometry(30, 10, 150, 20); pNameText->installEventFilter(this); pTextEditor = new QTextEdit(this); pTextEditor->setGeometry( QRect(0,0, 400, this->geometry().height() - 110) ); pTextEditor->move(500,44); pTextEditor->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); pTextEditor->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); pTextEditor->setStyleSheet("background-color:rgb(4,4,4)"); QPalette p = palette(); p.setColor(QPalette::Text, QColor(177, 177, 177)); pTextEditor->setPalette(p); // ????, ???widget????, ???ui->lineEdit?????. pTextEditor->show(); pLoadBtn = new QPushButton(this); pLoadBtn->setText("Load"); pLoadBtn->setGeometry( 410, 85, 80, 40 ); pSaveBtn = new QPushButton(this); pSaveBtn->setText("Save"); pSaveBtn->setGeometry( 410, 125, 80, 40 ); pSaveAsBtn = new QPushButton(this); pSaveAsBtn->setText("SaveAs"); pSaveAsBtn->setGeometry( 410, 165, 80, 40 ); pNewBtn = new QPushButton(this); pNewBtn->setText("CreateNew"); pNewBtn->setGeometry( 410, 205, 80, 40 ); pDelBtn = new QPushButton(this); pDelBtn->setText("Delete"); pDelBtn->setGeometry( 410, 245, 80, 40 ); pLoadBtn->show(); pSaveBtn->show(); pNewBtn->show(); pDelBtn->show(); connect( pLoadBtn, SIGNAL( clicked() ), this, SLOT( loadCall() ) ); connect( pSaveBtn, SIGNAL( clicked() ), this, SLOT( saveCall() ) ); connect( pNewBtn, SIGNAL( clicked() ), this, SLOT( newCall() ) ); connect( pDelBtn, SIGNAL( clicked() ), this, SLOT( deleteCall() ) ); connect( pSaveAsBtn, SIGNAL( clicked() ), this, SLOT( saveAsCall() ) ); findDlg = new QDialog(this); findDlg->setWindowTitle(tr("ęŸ„ę‰¾")); findLineEdit = new QLineEdit(findDlg); QPushButton *btn= new QPushButton(tr("ęŸ„ę‰¾äø‹äø€äøŖ"), findDlg); QVBoxLayout *layout= new QVBoxLayout(findDlg); layout->addWidget(findLineEdit); layout->addWidget(btn); connect(btn, SIGNAL(clicked()), this, SLOT(showFindText())); findDlg->hide(); //connect( this, SIGNAL( fileSelected(const QString &file) ), this, SLOT( fileSelectedChanged(const QString &file) ) ); QCoreApplication::postEvent( this, new QEvent( QEvent::Type( QEvent::User ) ) ); this->installEventFilter(this); currFile = ""; m_bModified = FALSE; } void spmFileLoadDialog::newCall() { QString fpath; int iCnt = 0; pRenameDialog->show(); fpath += this->curdirectory; fpath += "//NewFile"; fpath += '0' + iCnt; fpath += ".txt"; plog(GU(2),"spmFileLoadDialog newCall pathname:%s\n",fpath.toStdString().c_str() ); QFile *qfn = new QFile(); while(qfn->exists(fpath)) { ++ iCnt; fpath.clear(); fpath += this->curdirectory; fpath += "//NewFile"; fpath += '0' + iCnt; fpath += ".txt"; } qfn->setFileName(fpath); qfn->open(QIODevice::ReadWrite); qfn->close(); this->update(); this->updateGeometry(); selectedFiles().clear(); this->selectFile(fpath); // pRenameDialog->hide(); } void spmFileLoadDialog::showFindText() { } void spmFileLoadDialog::saveCall() { if(currLoadedFile == "") { QMessageBox::information(this,"Warning", "File Di Not Loaded?", QMessageBox::Ok ); } else if(QMessageBox::Yes==QMessageBox::information(this,"Warning", "Are you sure to Save?", QMessageBox::Yes,QMessageBox::No)) { QFile *saveFile = new QFile(currFile); saveFile->open(QIODevice::ReadWrite); saveFile->seek(-1); saveFile->write( QByteArray(pTextEditor->document()->toPlainText().toStdString().c_str() )); saveFile->close(); pTextEditor->document()->setModified(false); this->update(); this->updateGeometry(); } else { } } void spmFileLoadDialog::copyFile(QString fpath, QString fname) { QString path = fpath, cmd; int curr = 0; // while(1) { curr = path.lastIndexOf("/"); if(curr) { } else { ++ curr ; } } path.clear(); path = fpath.left(curr); path += "/"; path += fname; cmd.sprintf("/bin/cp -rp %s %s", fpath.toStdString().c_str(), path.toStdString().c_str()); plog(GU(1),"spmFileLoadDialog execute copyFilename=%s cmd:%s\n", fname.toStdString().c_str(),cmd.toStdString().c_str() ); system(cmd.toStdString().c_str()); this->selectFile(path); } void spmFileLoadDialog::renameFile(QString fpath, QString fname) { QString path = fpath, cmd; int curr = 0; // while(1) { curr = path.lastIndexOf("/"); if(curr) { } else { ++ curr ; } } path.clear(); path = fpath.left(curr); path += "/"; path += fname; cmd.sprintf("/bin/mv %s %s", fpath.toStdString().c_str(), path.toStdString().c_str()); plog(GU(1),"spmFileLoadDialog execute renameFilename=%s cmd:%s\n", fname.toStdString().c_str(),cmd.toStdString().c_str() ); system(cmd.toStdString().c_str()); this->selectFile(path); } void spmFileLoadDialog::hideRenameDialog() { pNameText->document()->setModified(TRUE); //rename the current selected file this->selectFile(fpath); pNameText->update(); qDebug("### hideRenameDialog par0:%s par1:%s### currTxt:%s", ((QString)(selectedFiles()[0])).toStdString().c_str(), pNameText->document()->toPlainText() ); //rename the current selected file this->selectFile(fpath); copyFile(this->selectedFiles()[0], pNameText->document()->toPlainText()); //renameFile(this->selectedFiles()[0], pNameText->document()->toPlainText()); pRenameDialog->hide(); this->update(); } int spmFileLoadDialog::showRenameDialog() { QPoint pos; //pos = this->mapToParent(pos); pos = this->mapToGlobal(pos); pRenameDialog->setWindowTitle("FileName:"); pRenameDialog->setGeometry(pos.x()+geometry().width()/2, pos.y()+geometry().height()/3, geometry().width()/2, geometry().height()/3); pRenameDialog->move( pos.x()+geometry().width()/2, pos.y()+geometry().height()/3 ); pRenameDialog->show(); return 0; } void spmFileLoadDialog::saveAsCall() { showRenameDialog(); m_bModified = 3; if(QMessageBox::Yes==QMessageBox::information(this,"Warning", "Are you sure to Save?", QMessageBox::Yes,QMessageBox::No)) { pTextEditor->document()->setModified(false); } else { } } void spmFileLoadDialog::loadCall() { QString fileBuf; QFile curloader; if(pTextEditor->document()->isModified()) { if(QMessageBox::No==QMessageBox::information(this,"Warning", "Current File is modified ,\nAre you sure to load another file?", QMessageBox::Yes,QMessageBox::No)) { return; } } fileBuf.clear(); curloader.setFileName(currFile); curloader.open(QIODevice::ReadWrite); fileBuf.append(curloader.readAll()); //plog(GU(1),"spmFileLoadDialog loadCall buffer:%s\n",fileBuf.toStdString().c_str() ); pTextEditor->clear(); pTextEditor->setText(fileBuf); pTextEditor->update(); currLoadedFile = currFile; } void spmFileLoadDialog::deleteCall() { if(QMessageBox::Yes==QMessageBox::information(this,"Warning", "Are you sure to Delete?", QMessageBox::Yes,QMessageBox::No)) { QString currFile = this->selectedFiles()[0]; QString cmdstr; cmdstr.sprintf("/bin/rm -rf %s", currFile.toStdString().c_str()); plog(GU(2),"spmFileLoadDialog deleteCall cmdstr:%s\n",cmdstr.toStdString().c_str() ); system(cmdstr.toStdString().c_str()); this->update(); this->updateGeometry(); } else { } } void spmFileLoadDialog::fileSelectedChanged(const QString &file) { QString currFile = this->selectedFiles()[0]; plog(GU(1),"fileSelectedChanged Current Selected file:%s\n", currFile.toStdString().c_str()); } void spmFileLoadDialog::customEvent(QEvent *e) { this->setGeometry(this->geometry().x(), this->geometry().y(), 900, this->geometry().height()); this->setViewMode(QFileDialog::Detail); this->setFileMode(QFileDialog::AnyFile); } spmFileLoadDialog::~spmFileLoadDialog() { } void spmFileLoadDialog::done(int result) { QFileDialog::done(result); } void spmFileLoadDialog::accept() { QFileDialog::accept(); } void spmFileLoadDialog::changeEvent(QEvent *event) {//Read the content from the path currFile = this->selectedFiles()[0]; //plog(GU(1),"Current Selected file:%s\n", currFile.toStdString().c_str()); QFileDialog::changeEvent(event); } void spmFileLoadDialog::showEvent(QShowEvent *event) { return QFileDialog::showEvent(event); } void spmFileLoadDialog::resizeEvent(QResizeEvent *event) { //QRect rect = this->geometry(); int xEdStartPos = this->geometry().width()/2; int xBtStartPos = this->geometry().width()/2 - 90; pLoadBtn->setGeometry( xBtStartPos, 85, 80, 40 ); pSaveBtn->setGeometry( xBtStartPos, 125, 80, 40 ); pSaveAsBtn->setGeometry( xBtStartPos, 165, 80, 40 ); pNewBtn->setGeometry( xBtStartPos, 205, 80, 40 ); pDelBtn->setGeometry( xBtStartPos, 245, 80, 40 ); pTextEditor->setGeometry( QRect( xEdStartPos, 44, this->geometry().width()/2-45, this->geometry().height() - 115) ); return QFileDialog::resizeEvent(event); } bool spmFileLoadDialog::eventFilter(QObject *obj, QEvent *event) { if(obj == this->pTextEditor) { if(event->type() == QEvent::KeyPress) { } } if( obj == pNameText ) { //plog(GU(2),"EventFilter pNameText event!\n"); if(event->type() == QEvent::KeyPress) { int key = static_cast<QKeyEvent*>(event)->key(); switch(key) { case Qt::Key_Enter: case Qt::Key_Return: hideRenameDialog(); break; default: break; } } } changeEvent(event); QFileDialog::eventFilter(obj, event); } |