mirror of
https://github.com/webodf/WebODF.git
synced 2026-11-30 16:47:58 +01:00
146 lines
4.0 KiB
C++
146 lines
4.0 KiB
C++
#include "mainwindow.h"
|
|
#include "ui_mainwindow.h"
|
|
#include "odfview.h"
|
|
#include <QtGui/QFileDialog>
|
|
#include <QtGui/QMdiArea>
|
|
#include <QtGui/QFileSystemModel>
|
|
#include <QtGui/QTreeView>
|
|
#include <QtGui/QDockWidget>
|
|
#include <QtCore/QDebug>
|
|
|
|
MainWindow::MainWindow(QWidget *parent) :
|
|
QMainWindow(parent),
|
|
ui(new Ui::MainWindow)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
createActions();
|
|
createToolBars();
|
|
|
|
setWindowTitle(tr("Odf Viewer"));
|
|
setUnifiedTitleAndToolBarOnMac(true);
|
|
|
|
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);
|
|
}
|
|
QString rootpath = "/home/oever/work/odfkit/odfkit/javascriptzip/kofficetests";
|
|
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)));
|
|
/*
|
|
OdfView *child = createOdfView();
|
|
if (child->loadFile("../kofficetests/odf/DanskTest01.odt")) {
|
|
child->showMaximized();
|
|
}
|
|
*/
|
|
}
|
|
|
|
MainWindow::~MainWindow()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void MainWindow::changeEvent(QEvent *e)
|
|
{
|
|
QMainWindow::changeEvent(e);
|
|
switch (e->type()) {
|
|
case QEvent::LanguageChange:
|
|
ui->retranslateUi(this);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
void MainWindow::open()
|
|
{
|
|
QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(),
|
|
tr("Office Files (*.odt *.odp *.ods)"));
|
|
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);
|
|
child->showMaximized();
|
|
} 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()
|
|
{
|
|
OdfView *view = new OdfView(this);
|
|
ui->mdiArea->addSubWindow(view);
|
|
return view;
|
|
}
|
|
|
|
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();
|
|
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);
|
|
}
|