#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
showFindDialog = true;
menuBar = new QMenuBar(this);
toolBar = new QToolBar(this);
textEdit = new QTextEdit(this);
fileMenu = new QMenu(menuBar); fileMenu->setTitle("文件");
editMenu = new QMenu(menuBar); editMenu->setTitle("编辑");
textMenu = new QMenu(menuBar); textMenu->setTitle("文本");
fontColorMenu = new QMenu(menuBar); fontColorMenu->setTitle("字体和颜色");
helpMenu = new QMenu(menuBar); helpMenu->setTitle("帮助");
newAction = new QAction(QIcon(":/image/Icon/new.ico"), "新建", this);
openAction = new QAction(QIcon(":/image/Icon/open.ico"), "打开", this);
saveAction = new QAction(QIcon(":/image/Icon/save.ico"), "保存", this);
saveAsAction = new QAction(QIcon(":/image/Icon/saveAs.ico"), "另存为", this);
modeAction = new QAction(QIcon(":/image/Icon/write.ico"), "编辑模式", this);
exitAction = new QAction("退出", this);
redoAction = new QAction("恢复", this);
cutAction = new QAction(QIcon(":/image/Icon/cut.ico"), "剪切", this);
copyAction = new QAction(QIcon(":/image/Icon/copy.ico"), "复制", this);
pasteAction = new QAction(QIcon(":/image/Icon/paste.ico"), "粘贴", this);
searchAction = new QAction("查找", this);
selectAllAction = new QAction("全选", this);
dateAction = new QAction("日期", this);
colorAction = new QAction("颜色", this);
fontAction = new QAction("字体", this);
imageAction = new QAction(QIcon(":/image/Icon/image.png"), "图片", this);
helpAction = new QAction("帮助", this);
aboutAction = new QAction("关于记事本", this);
boldAction = new QAction(QIcon(":/image/Icon/bold.png"), "粗体", this);
italicAction = new QAction(QIcon(":/image/Icon/italic.png"), "斜体", this);
underAction = new QAction(QIcon(":/image/Icon/under.png"), "下划线", this);
leftAction = new QAction(QIcon(":/image/Icon/left.png"), "左对齐", this);
centerAction = new QAction(QIcon(":/image/Icon/center.png"), "居中对齐", this);
rightAction = new QAction(QIcon(":/image/Icon/right.png"), "右对齐", this);
justifyAction= new QAction(QIcon(":/image/Icon/justify.png"), "整体排版", this);
newAction->setShortcut(QKeySequence::New);
openAction->setShortcut(QKeySequence::Open);
saveAction->setShortcut(QKeySequence::Save);
saveAsAction->setShortcut(QKeySequence::SaveAs);
modeAction->setShortcut(QKeySequence::NextChild);
redoAction->setShortcut(QKeySequence::Redo);
cutAction->setShortcut(QKeySequence::Cut);
copyAction->setShortcut(QKeySequence::Copy);
pasteAction->setShortcut(QKeySequence::Paste);
searchAction->setShortcut(QKeySequence::Find);
selectAllAction->setShortcut(QKeySequence::SelectAll);
dateAction->setShortcut(QKeySequence::Refresh);
dateAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_D));
colorAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_C));
fontAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_F));
imageAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_I));
boldAction->setShortcut(QKeySequence::Bold);
italicAction->setShortcut(QKeySequence::Italic);
underAction->setShortcut(QKeySequence::Underline);
fileMenu->addAction(newAction);
fileMenu->addAction(openAction);
fileMenu->addAction(saveAction);
fileMenu->addAction(saveAsAction);
fileMenu->addSeparator();
fileMenu->addAction(modeAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
editMenu->addAction(redoAction);
editMenu->addSeparator();
editMenu->addAction(cutAction);
editMenu->addAction(copyAction);
editMenu->addAction(pasteAction);
editMenu->addSeparator();
editMenu->addAction(searchAction);
editMenu->addSeparator();
editMenu->addAction(selectAllAction);
editMenu->addAction(dateAction);
textMenu->addAction(boldAction);
textMenu->addAction(italicAction);
textMenu->addAction(underAction);
textMenu->addSeparator();
textMenu->addAction(leftAction);
textMenu->addAction(centerAction);
textMenu->addAction(rightAction);
textMenu->addAction(justifyAction);
fontColorMenu->addAction(colorAction);
fontColorMenu->addAction(fontAction);
fontColorMenu->addAction(imageAction);//添加图片是后来添加上去的,所以命名有点不对劲.
helpMenu->addAction(helpAction);
helpMenu->addAction(aboutAction);
menuBar->addAction(fileMenu->menuAction());
menuBar->addAction(editMenu->menuAction());
menuBar->addAction(textMenu->menuAction());
menuBar->addAction(fontColorMenu->menuAction());
menuBar->addAction(helpMenu->menuAction());
toolBar->addAction(newAction);
toolBar->addAction(openAction);
toolBar->addAction(saveAction);
toolBar->addAction(saveAsAction);
toolBar->addSeparator();
toolBar->addAction(modeAction);
toolBar->addAction(imageAction);
toolBar->addSeparator();
toolBar->addAction(copyAction);
toolBar->addAction(pasteAction);
toolBar->addAction(cutAction);
toolBar->addSeparator();
toolBar->addAction(boldAction);
toolBar->addAction(italicAction);
toolBar->addAction(underAction);
toolBar->addSeparator();
toolBar->addAction(leftAction);
toolBar->addAction(centerAction);
toolBar->addAction(rightAction);
toolBar->addAction(justifyAction);
toolBar->setMovable(false);
toolBar->setContextMenuPolicy(Qt::PreventContextMenu);
findDialog = new FindDialog(this);
findDialog->hide();
connect(findDialog, SIGNAL(findText(QString, bool, bool)), this, SLOT(searchSlot(QString, bool, bool)));
connect(findDialog, SIGNAL(setShowFindDialog()), this, SLOT(setShowFindDialog()));
connect(newAction, SIGNAL(triggered()), this, SLOT(newSlot()));
connect(openAction, SIGNAL(triggered()), this, SLOT(openSlot()));
connect(saveAction, SIGNAL(triggered()), this, SLOT(saveSlot()));
connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAsSlot()));
connect(modeAction, SIGNAL(triggered()), this, SLOT(setModeSlot()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(redoAction, SIGNAL(triggered()), textEdit, SLOT(redo()));
connect(cutAction, SIGNAL(triggered()), textEdit, SLOT(cut()));
connect(copyAction, SIGNAL(triggered()), textEdit, SLOT(copy()));
connect(pasteAction, SIGNAL(triggered()), textEdit, SLOT(paste()));
connect(searchAction, SIGNAL(triggered()), this, SLOT(openSearchSlot()));
connect(selectAllAction, SIGNAL(triggered()), textEdit, SLOT(selectAll()));
connect(dateAction, SIGNAL(triggered()), this, SLOT(dateSlot()));
connect(colorAction, SIGNAL(triggered()), this, SLOT(colorSlot()));
connect(fontAction, SIGNAL(triggered()), this, SLOT(fontSlot()));
connect(imageAction, SIGNAL(triggered()), this, SLOT(imageSlot()));
connect(helpAction, SIGNAL(triggered()), this, SLOT(helpSlot()));
connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutSlot()));
connect(boldAction, SIGNAL(triggered()), this, SLOT(boldSlot()));
connect(italicAction, SIGNAL(triggered()), this, SLOT(italicSlot()));
connect(underAction, SIGNAL(triggered()), this, SLOT(underSlot()));
connect(leftAction, SIGNAL(triggered()), this, SLOT(leftSlot()));
connect(centerAction, SIGNAL(triggered()), this, SLOT(centerSlot()));
connect(rightAction, SIGNAL(triggered()), this, SLOT(rightSlot()));
connect(justifyAction, SIGNAL(triggered()), this, SLOT(justifySlot()));
textEdit->setFont(QFont("宋体", 13, QFont::Normal));
textEdit->setAcceptDrops(false);
leftAction->s