181 lines
5.6 KiB
C++
181 lines
5.6 KiB
C++
#include "DialogSaveFile.h"
|
|
#include "ui_dialogsavefile.h"
|
|
#include <QDir>
|
|
#include <QStringListModel>
|
|
#include <SysPara.h>
|
|
#include <QAbstractItemView>
|
|
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QMessageBox>
|
|
#include <Public.h>
|
|
#include<QStandardItemModel>
|
|
#include <QTextCodec>
|
|
#include <QByteArray>
|
|
#include <QDebug>
|
|
|
|
DialogSaveFile::DialogSaveFile(QWidget *parent) :
|
|
QDialog(parent),
|
|
ui(new Ui::DialogSaveFile)
|
|
{
|
|
ui->setupUi(this);
|
|
this->setWindowTitle(tr("保存"));
|
|
ui->textEditView->setReadOnly(true);
|
|
ui->textEditView->setLineWrapMode(QTextEdit::NoWrap);
|
|
ui->textEditView->document()->setDocumentMargin(0);
|
|
this->setFixedSize(this->geometry().width(),this->geometry().height());
|
|
EditMode=4;
|
|
|
|
ui->textEditView->hide();
|
|
ui->label->hide();
|
|
ui->pushButtonLast->hide();
|
|
ui->pushButtonNext->hide();
|
|
|
|
}
|
|
|
|
void DialogSaveFile::refreshListView(int mode){
|
|
|
|
QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_DOC_ROOT_DIR;
|
|
QDir dir(dirPath);
|
|
QStringList filters;
|
|
EditMode = mode;
|
|
if(mode == TXTFILE) filters << "*.html" << "*.txt" << "*.TXT" << "*.jpeg" << "*.gif" << "*.jpg" << "*.bmp"<< "*.BMP"; // txtmode
|
|
else if(mode == PICFILE) filters << "*.png";// <<"*.jpg" ; // picmode
|
|
else if(mode == SCENE) filters << "*.xml";// scene mode
|
|
else if(mode == 4) filters << "*.png" << "*.jpg" << "*.jpeg" << "*.bmp" << "*.BMP" << "*.gif";
|
|
else filters << "*.html" << "*.bmp" << "*.BMP" <<"*.jpg"<<"*.gif";
|
|
dir.setNameFilters(filters);
|
|
dir.setFilter(QDir::Files);
|
|
|
|
QStringListModel *model = new QStringListModel(dir.entryList());
|
|
|
|
ui->listViewFile->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
ui->listViewFile->setModel(model); //useList是个QListView
|
|
int count = ui->listViewFile->model()->rowCount();
|
|
if(count){
|
|
QModelIndex index = ui->listViewFile->model()->index(0 , 0);
|
|
ui->listViewFile->setCurrentIndex(index);
|
|
on_listViewFile_entered(index);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
DialogSaveFile::~DialogSaveFile()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void DialogSaveFile::on_listViewFile_entered(const QModelIndex &index)
|
|
{
|
|
QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_DOC_ROOT_DIR;
|
|
|
|
QString filePath = dirPath + index.data().toString();
|
|
|
|
if(filePath.endsWith(".gif") || filePath.endsWith(".jpeg") || filePath.endsWith(".jpg") ||
|
|
filePath.endsWith(".png") || filePath.endsWith(".bmp")|| filePath.endsWith(".BMP")|| filePath.endsWith(".nda")){
|
|
ui->textEditView->clear();
|
|
ui->textEditView->setHtml(QString("<img src='%1'/>").arg(filePath));
|
|
}
|
|
else {
|
|
QFile file(filePath);
|
|
if (!file.open(QFile::ReadOnly))
|
|
return ;
|
|
QByteArray data = file.readAll();
|
|
QTextCodec *codec = Qt::codecForHtml(data);
|
|
QString str = codec->toUnicode(data);
|
|
if (Qt::mightBeRichText(str)) {
|
|
if(!ui->radioButtonLocal->isChecked())
|
|
str.replace(DOC_ROOT_DIR,UDISK_DOC_ROOT_DIR); //必须替换相应的路径
|
|
ui->textEditView->setHtml(str);
|
|
} else {
|
|
str = QString::fromLocal8Bit(data);
|
|
ui->textEditView->setPlainText(str);
|
|
}
|
|
}
|
|
|
|
ui->lineEdit->setText( QFileInfo(ui->listViewFile->currentIndex().data().toString()).baseName() );
|
|
|
|
}
|
|
void DialogSaveFile::on_listViewFile_clicked(const QModelIndex &index)
|
|
{
|
|
on_listViewFile_entered(index);
|
|
}
|
|
|
|
void DialogSaveFile::on_pushButtonNext_pressed()
|
|
{
|
|
QModelIndex currIndex = ui->listViewFile->currentIndex();
|
|
int count = ui->listViewFile->model()->rowCount();
|
|
if(count){
|
|
QModelIndex index = ui->listViewFile->model()->index((currIndex.row() + 1)%count, 0);
|
|
ui->listViewFile->setCurrentIndex(index);
|
|
on_listViewFile_entered(index);
|
|
}
|
|
ui->lineEdit->setText(QFileInfo(ui->listViewFile->currentIndex().data().toString()).baseName());
|
|
}
|
|
|
|
void DialogSaveFile::on_pushButtonLast_pressed()
|
|
{
|
|
QModelIndex currIndex = ui->listViewFile->currentIndex();
|
|
int count = ui->listViewFile->model()->rowCount();
|
|
if(count){
|
|
QModelIndex index = ui->listViewFile->model()->index(currIndex.row() == 0?(count - 1):(currIndex.row() - 1), 0);
|
|
ui->listViewFile->setCurrentIndex(index);
|
|
on_listViewFile_entered(index);
|
|
}
|
|
ui->lineEdit->setText(QFileInfo(ui->listViewFile->currentIndex().data().toString()).baseName());
|
|
}
|
|
|
|
void DialogSaveFile::on_pushButtonExit_pressed()
|
|
{
|
|
hide();
|
|
}
|
|
//void DialogSaveFile::on_lostfocuse_pressed()
|
|
//{
|
|
// hide();
|
|
//}
|
|
void DialogSaveFile::on_pushButtonSave_clicked()
|
|
{
|
|
QModelIndex index = ui->listViewFile->currentIndex();
|
|
QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_DOC_ROOT_DIR;
|
|
//QString filePath = dirPath + index.data().toString();
|
|
QString filePath = dirPath + ui->lineEdit->text();// index.data().toString();
|
|
|
|
emit saveFile(filePath,false);
|
|
|
|
emit returnMainWidget(20);
|
|
hide();
|
|
}
|
|
|
|
void DialogSaveFile::on_radioButtonLocal_released()
|
|
{
|
|
refreshListView(EditMode);
|
|
}
|
|
|
|
void DialogSaveFile::on_radioButtonUdisk_released()
|
|
{
|
|
|
|
QDir dir(UDISK_DOC_ROOT_DIR);
|
|
if(!dir.exists()){
|
|
QMessageBox::warning(0,tr("警告"),tr("U盘未插入或U盘根目录下的doc文件夹不存在!"));
|
|
}
|
|
refreshListView(EditMode);
|
|
}
|
|
void DialogSaveFile::setEnterFileName(QString strFile)
|
|
{
|
|
ui->lineEdit->setText(strFile);
|
|
//ui->lineEdit->setFocus();
|
|
}
|
|
void DialogSaveFile::setTitleTxt(QString str)
|
|
{
|
|
setWindowTitle(str);
|
|
}
|
|
|
|
void DialogSaveFile::saveLastWork()
|
|
{
|
|
QString dirPath = ui->radioButtonLocal->isChecked()?DOC_ROOT_DIR:UDISK_DOC_ROOT_DIR;
|
|
QString filePath = dirPath + "LastWork.xml";
|
|
|
|
emit saveFile(filePath,false);
|
|
}
|
|
|