2010-06-17 17:24:36 +02:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
#include "ui_mainwindow.h"
|
2010-06-17 18:30:32 +02:00
|
|
|
#include "odfview.h"
|
|
|
|
|
#include <QtGui/QFileDialog>
|
|
|
|
|
#include <QtGui/QMdiArea>
|
2010-06-22 14:43:03 +02:00
|
|
|
#include <QtGui/QFileSystemModel>
|
|
|
|
|
#include <QtGui/QTreeView>
|
|
|
|
|
#include <QtGui/QDockWidget>
|
2010-06-24 10:48:55 +02:00
|
|
|
#include <QtCore/QDir>
|
2010-06-22 14:43:03 +02:00
|
|
|
#include <QtCore/QDebug>
|
2010-06-17 17:24:36 +02:00
|
|
|
|
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
|
|
|
QMainWindow(parent),
|
|
|
|
|
ui(new Ui::MainWindow)
|
|
|
|
|
{
|
|
|
|
|
ui->setupUi(this);
|
2010-06-17 18:30:32 +02:00
|
|
|
|
|
|
|
|
createActions();
|
|
|
|
|
createToolBars();
|
|
|
|
|
|
2010-06-22 14:43:03 +02:00
|
|
|
setWindowTitle(tr("Odf Viewer"));
|
2010-06-17 18:30:32 +02:00
|
|
|
setUnifiedTitleAndToolBarOnMac(true);
|
2010-06-18 23:04:31 +02:00
|
|
|
|
2010-06-22 14:43:03 +02:00
|
|
|
QStringList odfNameFilter;
|
|
|
|
|
odfNameFilter << "*.odt" << "*.ods" << "*.odp";
|
|
|
|
|
dirmodel = new QFileSystemModel(this);
|
|
|
|
|
dirmodel->setNameFilters(odfNameFilter);
|
|
|
|
|
dirmodel->setFilter(QDir::AllDirs|QDir::AllEntries|QDir::NoDotAndDotDot);
|
|
|
|
|
dirview = new QTreeView(this);
|
|
|
|
|
dirview->setModel(dirmodel);
|
|
|
|
|
dirview->setHeaderHidden(true);
|
|
|
|
|
dirview->setAnimated(true);
|
|
|
|
|
for (int i = 1; i < dirmodel->columnCount(); i++) {
|
|
|
|
|
dirview->setColumnHidden(i, true);
|
|
|
|
|
}
|
2010-06-24 10:48:55 +02:00
|
|
|
QString rootpath = QDir::homePath();
|
2010-06-22 14:43:03 +02:00
|
|
|
dirmodel->setRootPath(rootpath);
|
|
|
|
|
const QModelIndex rootindex = dirmodel->index(rootpath);
|
|
|
|
|
dirview->setRootIndex(rootindex);
|
|
|
|
|
dirdock = new QDockWidget(this);
|
|
|
|
|
dirdock->setWidget(dirview);
|
|
|
|
|
addDockWidget(Qt::LeftDockWidgetArea, dirdock);
|
|
|
|
|
|
|
|
|
|
connect(dirview, SIGNAL(clicked(QModelIndex)), this, SLOT(loadOdf(QModelIndex)));
|
|
|
|
|
/*
|
2010-06-18 23:04:31 +02:00
|
|
|
OdfView *child = createOdfView();
|
|
|
|
|
if (child->loadFile("../kofficetests/odf/DanskTest01.odt")) {
|
|
|
|
|
child->showMaximized();
|
|
|
|
|
}
|
2010-06-22 14:43:03 +02:00
|
|
|
*/
|
2010-06-17 17:24:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MainWindow::~MainWindow()
|
|
|
|
|
{
|
|
|
|
|
delete ui;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-24 10:48:55 +02:00
|
|
|
void
|
|
|
|
|
MainWindow::openFile(const QString& path)
|
|
|
|
|
{
|
|
|
|
|
QMdiSubWindow* w = findMdiChild(path);
|
|
|
|
|
OdfView* v = (w) ?dynamic_cast<OdfView*>(w->widget()) :0;
|
|
|
|
|
if (v == 0) {
|
|
|
|
|
w = ui->mdiArea->activeSubWindow();
|
|
|
|
|
v = (w) ?dynamic_cast<OdfView*>(w->widget()) :0;
|
|
|
|
|
}
|
|
|
|
|
if (v == 0) {
|
|
|
|
|
v = new OdfView(this);
|
|
|
|
|
v->showMaximized();
|
|
|
|
|
w = ui->mdiArea->addSubWindow(v);
|
|
|
|
|
w->showMaximized();
|
|
|
|
|
}
|
|
|
|
|
ui->mdiArea->setActiveSubWindow(w);
|
|
|
|
|
v->loadFile(path);
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-17 17:24:36 +02:00
|
|
|
void MainWindow::changeEvent(QEvent *e)
|
|
|
|
|
{
|
|
|
|
|
QMainWindow::changeEvent(e);
|
|
|
|
|
switch (e->type()) {
|
|
|
|
|
case QEvent::LanguageChange:
|
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-06-17 18:30:32 +02:00
|
|
|
void MainWindow::open()
|
|
|
|
|
{
|
2010-06-22 14:43:03 +02:00
|
|
|
QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(),
|
|
|
|
|
tr("Office Files (*.odt *.odp *.ods)"));
|
2010-06-17 18:30:32 +02:00
|
|
|
if (!fileName.isEmpty()) {
|
|
|
|
|
QMdiSubWindow *existing = findMdiChild(fileName);
|
|
|
|
|
if (existing) {
|
|
|
|
|
ui->mdiArea->setActiveSubWindow(existing);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OdfView *child = createOdfView();
|
|
|
|
|
if (child->loadFile(fileName)) {
|
|
|
|
|
statusBar()->showMessage(tr("File loaded"), 2000);
|
2010-06-18 23:04:31 +02:00
|
|
|
child->showMaximized();
|
2010-06-17 18:30:32 +02:00
|
|
|
} else {
|
|
|
|
|
child->close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
void MainWindow::createActions()
|
|
|
|
|
{
|
|
|
|
|
openAct = new QAction(QIcon(":/images/open.png"), tr("&Open..."), this);
|
|
|
|
|
openAct->setShortcuts(QKeySequence::Open);
|
|
|
|
|
openAct->setStatusTip(tr("Open an existing file"));
|
|
|
|
|
connect(openAct, SIGNAL(triggered()), this, SLOT(open()));
|
|
|
|
|
}
|
|
|
|
|
void MainWindow::createToolBars()
|
|
|
|
|
{
|
|
|
|
|
fileToolBar = addToolBar(tr("File"));
|
|
|
|
|
fileToolBar->addAction(openAct);
|
|
|
|
|
}
|
|
|
|
|
QMdiSubWindow *MainWindow::findMdiChild(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
QString canonicalFilePath = QFileInfo(fileName).canonicalFilePath();
|
|
|
|
|
|
|
|
|
|
foreach (QMdiSubWindow *window, ui->mdiArea->subWindowList()) {
|
|
|
|
|
OdfView *odfView = qobject_cast<OdfView *>(window->widget());
|
|
|
|
|
if (odfView->currentFile() == canonicalFilePath)
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OdfView *MainWindow::createOdfView()
|
|
|
|
|
{
|
2010-06-22 14:43:03 +02:00
|
|
|
OdfView *view = new OdfView(this);
|
2010-06-17 18:30:32 +02:00
|
|
|
ui->mdiArea->addSubWindow(view);
|
|
|
|
|
return view;
|
|
|
|
|
}
|
2010-06-22 14:43:03 +02:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
MainWindow::loadOdf(const QModelIndex& index) {
|
|
|
|
|
if (dirmodel->isDir(index)) {
|
|
|
|
|
if (dirview->isExpanded(index)) {
|
|
|
|
|
dirview->collapse(index);
|
|
|
|
|
} else {
|
|
|
|
|
dirview->expand(index);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
QString path = dirmodel->filePath(index);
|
|
|
|
|
path = QFileInfo(path).canonicalFilePath();
|
2010-06-24 10:48:55 +02:00
|
|
|
openFile(path);
|
2010-06-22 14:43:03 +02:00
|
|
|
}
|