#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets/QLabel>
#include "finddialog.h"
#include "gotocelldialog.h"
#include "sortdialog.h"
#include "spreadsheet.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QCloseEvent>
#include "ui_gotocelldialog.h"
#include "ui_sortdialog.h"
#include <QSettings>
#include <QBitArray>
//functions
void MainWindow::createActions()
{
for(int i=0; i<MaxRecentFiles; ++i)
{
recentFileActions[i] = new QAction(this);
recentFileActions[i]->setVisible(false);
connect(recentFileActions[i], SIGNAL(triggered()), this, SLOT(openRecentFile()));
}
connect(ui->action_New, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->action_Exit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->action_Open, SIGNAL(triggered()), this, SLOT(open()));
connect(ui->action_Save, SIGNAL(triggered()), this, SLOT(save()));
connect(ui->actionSave_As, SIGNAL(triggered()), this, SLOT(saveAs()));
connect(ui->action_All, SIGNAL(triggered()), this, SLOT(selectAll()));
connect(ui->actionShow_Grid, SIGNAL(triggered(bool)), this, SLOT(setShowGrid()));
connect(ui->action_AboutQt, SIGNAL(triggered()), this, SLOT(aboutQt()));
connect(ui->action_About, SIGNAL(triggered()), this, SLOT(about()));
connect(ui->action_Find, SIGNAL(triggered()), this, SLOT(find()));
connect(ui->action_Go_To_Cell, SIGNAL(triggered()), this, SLOT(goToCell()));
connect(ui->action_Sort, SIGNAL(triggered()), this, SLOT(sort()));
connect(ui->action_Exit, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
}
void MainWindow::createMenus()
{
cutAction = new QAction(tr("Cu&t"), this);
copyAction = new QAction(tr("&Copy"), this);
pasteAction = new QAction(tr("&Paste"), this);
//
for(int i=0; i<MaxRecentFiles; ++i)
{
ui->menu_File->addAction(recentFileActions[i]->text());
}
}
void MainWindow::createContextMenu()
{
spreadSheet->addAction(cutAction);
spreadSheet->addAction(copyAction);
spreadSheet->addAction(pasteAction);
spreadSheet->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void MainWindow::createToolBars()
{
ui->fileToolBar->addAction(ui->action_New);
ui->fileToolBar->addAction(ui->action_Open);
ui->fileToolBar->addAction(ui->action_Save);
ui->editToolBar->addAction(ui->action_Cut);
ui->editToolBar->addAction(ui->action_Copy);
ui->editToolBar->addAction(ui->action_Paste);
ui->editToolBar->addSeparator();
ui->editToolBar->addAction(ui->action_Find);
ui->editToolBar->addAction(ui->action_Go_To_Cell);
ui->editToolBar->addAction(ui->action_AboutQt);
addToolBar(ui->fileToolBar);
addToolBar(ui->editToolBar);
}
void MainWindow::createStatusBar()
{
locationLabel = new QLabel("W999");
locationLabel->setAlignment(Qt::AlignHCenter);
locationLabel->setMinimumSize(locationLabel->sizeHint());
formulaLabel = new QLabel;
formulaLabel->setIndent(3);
ui->mainStatusBar->addWidget(locationLabel);
ui->mainStatusBar->addWidget(formulaLabel);
connect(spreadSheet, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(updateStatusBar()));
connect(spreadSheet, SIGNAL(modified()), this, SLOT(spreadsheetModified()));
updateStatusBar();
}
void MainWindow::readSettings()
{
QSettings settings("Software Inc.", "SpreadSheet");
restoreGeometry(settings.value("geometry").toByteArray());
recentFiles = settings.value("recentfiles").toStringList();
updateRecentFileActions();
bool showgrid = settings.value("showgrid", true).toBool();
ui->actionShow_Grid->setChecked(showgrid);
bool autorecalc = settings.value("autorecalc", true).toBool();
ui->actionAuto_Recalculate->setChecked(autorecalc);
}
void MainWindow::writeSettings()
{
QSettings settings("Software Inc.", "SpreadSheet");
settings.setValue("geometry", saveGeometry());
settings.setValue("recentfiles", recentFiles);
settings.setValue("showgrid", ui->actionShow_Grid->isChecked());
settings.setValue("autorecalc", ui->actionAuto_Recalculate->isChecked());
}
void MainWindow::setCurrentFile(const QString &fileName)
{
curFile = fileName;
setWindowModified(false);
QString shownName = tr("Untitled");
if(!curFile.isEmpty())
{
shownName = strippedName(curFile);
recentFiles.removeAll(curFile);
recentFiles.prepend(curFile);
updateRecentFileActions();
}
setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("spread sheet")));
}
QString MainWindow::strippedName(const QString &fullFileName)
{
return QFileInfo(fullFileName).fileName();
}
void MainWindow::updateRecentFileActions()
{
QMutableStringListIterator i(recentFiles);
while (i.hasNext())
{
if(!QFile::exists(i.next()))
i.remove();
}
for(int j=0; j<MaxRecentFiles; ++j)
{
if(j < recentFiles.count())
{
QString text = tr("&%1 %2").arg(j+1).arg(strippedName(recentFiles[j]));
recentFileActions[j]->setText(text);
recentFileActions[j]->setData(recentFiles[j]);
recentFileActions[j]->setVisible(true);
}
else
{
recentFileActions[j]->setVisible(false);
}
}
spreadSheet->setVisible(!recentFiles.isEmpty());
}
//slots
void MainWindow::newFile()
{
if(okToContinue())
{
//spreadSheet->clear();
//setCurrentFile(tr(""));
MainWindow *w = new MainWindow;
w->show();
}
}
bool MainWindow::okToContinue()
{
if(isWindowModified())
{
int r = QMessageBox::warning(this, tr("SpreadSheet"),\
tr("the doc has been modfied.\ndo you want to save your changes?"),\
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if(r == QMessageBox::Yes)
{
return save();
}
else if(r == QMessageBox::Cancel)
{
return false;
}
}
return true;
}
void MainWindow::open()
{
if(okToContinue())
{
QString fileName = QFileDialog::getOpenFileName(this, tr("open spread sheet"),\
".", tr("spread sheet file (*.sp)"));
if(!fileName.isEmpty())
{
loadFile(fileName);
}
}
}
bool MainWindow::loadFile(const QString &fileName)
{
if(!spreadSheet->readFile(fileName))
{
ui->mainStatusBar->showMessage(tr("loading canceled"), 2000);
return false;
}
setCurrentFile(fileName);
ui->mainStatusBar->showMessage(tr("file loaded"), 2000);
return true;
}
bool MainWindow::save()
{
if(curFile.isEmpty())
{
return saveAs();
}
else
{
return saveFile(curFile);
}
}
bool MainWindow::saveFile(const QString &fileName)
{
if(!spreadSheet->writeFile(fileName))
{
ui->mainStatusBar->showMessage(tr("saving canceled"), 2000);
return false;
}
setCurrentFile(fileName);
ui->mainStatusBar->showMessage(tr("file saved"), 2000);
return true;
}
bool MainWindow::saveAs()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("save spread sheet"),\
".", tr("spread sheet file (*.sp)"));
if(fileName.isEmpty())
{
return false;
}
return saveFile(fileName);
}
void MainWindow::selectAll()
{
}
void MainWindow::setShowGrid()
{
}
void MainWindow::aboutQt()
{
QMessageBox::aboutQt(this, tr("Qt 5.0.1"));
}
void MainWindow::find()
{
if(!findDialog)
{
findDialog = new FindDialog(this);
C++ Gui Qt4 1~3章代码整理
4星 · 超过85%的资源 需积分: 10 99 浏览量
2013-07-22
10:03:28
上传
评论
收藏 86KB RAR 举报
momo-lan
- 粉丝: 0
- 资源: 5
最新资源
- 基于微信小程序+mysql的答题系统的设计与实现+全部资料+详细文档(毕业设计).zip
- H5U的PLC固件,用于EthernetIP通讯时升级固件使用
- Jumpserver堡垒机快速入门视频.zip
- 基于springboot+mysql+redis实现个性化书籍推荐和相似帖子推荐的社区图书馆+论坛+失物招领平台
- 软考中级软件设计师 法律法规通关笔记
- 基于java+sqlserver实现的员工培训系统
- java毕业设计基于Springboot的贫困生资助系统源码+使用文档+全部资料(优秀项目).zip
- 基于随机森林和袋装法对PM2.5浓度进行回归预测
- 静态路由实验.docx
- Rust语言探索之旅:从入门到实践,构建安全高效的系统级应用-文章博客-markdown材料.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈