#include "mainwindow.h"
#include <QtGui>
#include "finddialog.h"
#include "gotocelldialog.h"
#include "sortdialog.h"
#include "spreadsheet.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
spreadsheet = new Spreadsheet;
setCentralWidget(spreadsheet);
createActions();
createMenus();
createContextMenu();
createToolBars();
createStatusBar();
readSettings();
findDialog = 0;
setWindowIcon(QIcon(":/images/icon.png"));
setAttribute(Qt::WA_DeleteOnClose);
setCurrentFile("");
foreach (QWidget *win, QApplication::topLevelWidgets()) {
if (MainWindow *mainWin = qobject_cast<MainWindow *>(win))
mainWin->updateRecentFileActions();
}
}
MainWindow::~MainWindow()
{
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (okToContinue())
{
writeSettings();
event->accept();
}
else {
event->ignore();
}
}
void MainWindow::newFile()
{
/* if (okToContinue()) {
spreadsheet->clear();
setCurrentFile("");
}*/
MainWindow *mainWin = new MainWindow;
mainWin->show();
}
void MainWindow::open()
{
if (okToContinue()) {
QString fileName = QFileDialog::getOpenFileName(this,tr("Open Spreadsheet"), ".",tr("Spreadsheet files (*.sp)"));
if (!fileName.isEmpty())
loadFile(fileName);
}
}
bool MainWindow::save()
{
if (curFile.isEmpty())
{
return saveAs();
}
else
{
return saveFile(curFile);
}
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this,tr("Save Spreadsheet"), ".",tr("Spreadsheet files (*.sp)"));
if (fileName.isEmpty())
return false;
return saveFile(fileName);
}
void MainWindow::find()
{
if (!findDialog)
{
findDialog = new FindDialog(this);
connect(findDialog, SIGNAL(findNext(const QString &,Qt::CaseSensitivity)),spreadsheet, SLOT(findNext(const QString &,Qt::CaseSensitivity)));
connect(findDialog, SIGNAL(findPrevious(const QString &,Qt::CaseSensitivity)),spreadsheet, SLOT(findPrevious(const QString &,Qt::CaseSensitivity)));
}
findDialog->show();
findDialog->raise();
findDialog->activateWindow();
}
void MainWindow::goToCell()
{
GoToCellDialog dialog(this);
if (dialog.exec())
{
QString str = dialog.lineEdit->text().toUpper();
spreadsheet->setCurrentCell(str.mid(1).toInt() - 1,str[0].unicode() - 'A');
}
}
void MainWindow::sort()
{
SortDialog dialog(this);
QTableWidgetSelectionRange range = spreadsheet->selectedRange();
dialog.setColumnRange('A' + range.leftColumn(),'A' + range.rightColumn());
if (dialog.exec())
{
SpreadsheetCompare compare;
compare.keys[0] = dialog.primaryColumnCombo->currentIndex();
compare.keys[1] = dialog.secondaryColumnCombo->currentIndex() - 1;
compare.keys[2] = dialog.tertiaryColumnCombo->currentIndex() - 1;
compare.ascending[0] = (dialog.primaryOrderCombo->currentIndex() == 0);
compare.ascending[1] = (dialog.secondaryOrderCombo->currentIndex() == 0);
compare.ascending[2] = (dialog.tertiaryOrderCombo->currentIndex() == 0);
spreadsheet->sort(compare);
}
}
void MainWindow::about()
{
QMessageBox::about(this, tr("About Spreadsheet"),tr("<h2>Spreadsheet 1.1</h2>""<p>Copyright © 2006 SoftwareInc.""<p>Spreadsheet is a small application that ""demonstrates QAction, QMainWindow, QMenuBar, ""QStatusBar, QTableWidget, QToolBar, and many other ""Qt classes."));
}
void MainWindow::openRecentFile()
{
if (okToContinue()) {
QAction *action = qobject_cast<QAction *>(sender());
if (action)
loadFile(action->data().toString());
}
}
void MainWindow::updateStatusBar()
{
locationLabel->setText(spreadsheet->currentLocation());
formulaLabel->setText(spreadsheet->currentFormula());
}
void MainWindow::spreadsheetModified()
{
setWindowModified(true);
updateStatusBar();
}
void MainWindow::createActions()
{
newAction = new QAction(tr("&New"), this);
newAction->setIcon(QIcon(":/images/new.png"));
// newAction->setShortcut(tr("Ctrl+N"));
newAction->setShortcut(QKeySequence::New);
newAction->setStatusTip(tr("Create a new spreadsheet file"));
connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));
openAction = new QAction(tr("&Open..."), this);
openAction->setIcon(QIcon(":/images/open.png"));
openAction->setShortcut(tr("Ctrl+O"));
openAction->setStatusTip(tr("Open an existing spreadsheet file"));
connect(openAction, SIGNAL(triggered()), this, SLOT(open()));
saveAction = new QAction(tr("&Save"), this);
saveAction->setIcon(QIcon(":/images/save.png"));
saveAction->setShortcut(tr("Ctrl+S"));
saveAction->setStatusTip(tr("Save the spreadsheet to disk"));
connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));
saveAsAction = new QAction(tr("Save &As..."), this);
saveAsAction->setStatusTip(tr("Save the spreadsheet under a new ""name"));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));
for (int i = 0; i < MaxRecentFiles; ++i) {
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()),this, SLOT(openRecentFile()));
}
closeAction = new QAction(tr("&Close"), this);
closeAction->setShortcut(QKeySequence::Close);
closeAction->setStatusTip(tr("Close this window"));
connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
/* exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));*/
exitAction = new QAction(tr("E&xit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit the application"));
connect(exitAction, SIGNAL(triggered()),qApp, SLOT(closeAllWindows()));
cutAction = new QAction(tr("Cu&t"), this);
cutAction->setIcon(QIcon(":/images/cut.png"));
cutAction->setShortcut(tr("Ctrl+X"));
cutAction->setStatusTip(tr("Cut the current selection's contents ""to the clipboard"));
connect(cutAction, SIGNAL(triggered()), spreadsheet, SLOT(cut()));
copyAction = new QAction(tr("&Copy"), this);
copyAction->setIcon(QIcon(":/images/copy.png"));
copyAction->setShortcut(tr("Ctrl+C"));
copyAction->setStatusTip(tr("Copy the current selection's contents ""to the clipboard"));
connect(copyAction, SIGNAL(triggered()), spreadsheet, SLOT(copy()));
pasteAction = new QAction(tr("&Paste"), this);
pasteAction->setIcon(QIcon(":/images/paste.png"));
pasteAction->setShortcut(tr("Ctrl+V"));
pasteAction->setStatusTip(tr("Paste the clipboard's contents into ""the current selection"));
connect(pasteAction, SIGNAL(triggered()),spreadsheet, SLOT(paste()));
deleteAction = new QAction(tr("&Delete"), this);
deleteAction->setShortcut(tr("Del"));
deleteAction->setStatusTip(tr("Delete the current selection's ""contents"));
connect(deleteAction, SIGNAL(triggered()),spreadsheet, SLOT(del()));
selectRowAction = new QAction(tr("&Row"), this);
selectRowAction->setStatusTip(tr("
- 1
- 2
前往页