Skip to content
Permalink
Browse files

Merge pull request #21 from sandsmark/develop

some fixes
  • Loading branch information...
pbek committed Mar 11, 2019
2 parents d847561 + 08d8af2 commit d3d480d1c34f11e6e8e4d537cba612f3534eb526
Showing with 290 additions and 151 deletions.
  1. +6 −5 CMakeLists.txt
  2. +16 −0 main.cpp
  3. +87 −0 mainwindow.cpp
  4. +12 −1 mainwindow.h
  5. +158 −138 markdownhighlighter.cpp
  6. +11 −7 markdownhighlighter.h
@@ -3,7 +3,7 @@ project(qmarkdowntextedit)

#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

#set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
@@ -21,8 +21,8 @@ set(RESOURCE_FILES
qt5_add_resources(RESOURCE_ADDED ${RESOURCE_FILES})

set(SOURCE_FILES
highlighter.cpp
highlighter.h
markdownhighlighter.cpp
markdownhighlighter.h
main.cpp
mainwindow.cpp
mainwindow.h
@@ -31,7 +31,8 @@ set(SOURCE_FILES
qmarkdowntextedit.h
qplaintexteditsearchwidget.ui
qplaintexteditsearchwidget.cpp
qplaintexteditsearchwidget.h)
qplaintexteditsearchwidget.h
)

add_executable(qmarkdowntextedit ${SOURCE_FILES} ${RESOURCE_ADDED})

@@ -45,4 +46,4 @@ add_definitions(${Qt5Widgets_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")

# The Qt5Widgets_LIBRARIES variable also includes QtGui and QtCore
target_link_libraries(qmarkdowntextedit ${Qt5Widgets_LIBRARIES})
target_link_libraries(qmarkdowntextedit Qt5::Widgets)
@@ -14,12 +14,28 @@

#include "mainwindow.h"
#include <QApplication>
#include <QFileInfo>
#include <QDebug>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString filename;
if (argc > 1) {
filename = argv[1];
}
if (!filename.isEmpty() && !QFileInfo(filename).isReadable()) {
qWarning() << filename << "is not a readable file";
return 1;
}

MainWindow w;
w.show();

if (!filename.isEmpty()) {
w.loadFile(filename);
}


return a.exec();
}
@@ -17,15 +17,102 @@

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qmarkdowntextedit.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QToolBar *toolBar = new QToolBar;
addToolBar(toolBar);
QAction *openAction = new QAction(QIcon::fromTheme("document-open"), tr("Open..."));
openAction->setShortcut(QKeySequence::Open);
connect(openAction, &QAction::triggered, this, &MainWindow::open);

QAction *saveAction = new QAction(QIcon::fromTheme("document-save"), tr("Save"));
saveAction->setShortcut(QKeySequence::Save);
QAction *saveAsAction = new QAction(QIcon::fromTheme("document-save-as"), tr("Save as..."));
saveAsAction->setShortcut(QKeySequence::SaveAs);
QAction *quitAction = new QAction(QIcon::fromTheme("view-close"), tr("Quit"));
quitAction->setShortcut(QKeySequence::Quit);
connect(quitAction, &QAction::triggered, this, &MainWindow::onQuit);

m_loadedContent = ui->textEdit->toPlainText();

toolBar->addActions({openAction, saveAction, saveAsAction, quitAction});
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::loadFile(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open" << filename;
return;
}

m_filename = filename;
m_loadedContent = QString::fromLocal8Bit(file.readAll());
ui->textEdit->setPlainText(m_loadedContent);
}

void MainWindow::saveToFile(const QString &filename)
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to open" << filename;
return;
}

m_filename = filename;

m_loadedContent = ui->textEdit->toPlainText();
file.write(m_loadedContent.toLocal8Bit());
}

void MainWindow::open()
{
QString filename = QFileDialog::getOpenFileName();
if (filename.isEmpty()) {
return;
}
loadFile(filename);
}

void MainWindow::save()
{
if (!m_filename.isEmpty()) {
saveAs();
return;
}

saveToFile(m_filename);
}

void MainWindow::saveAs()
{
QString filename = QFileDialog::getSaveFileName();
if (filename.isEmpty()) {
return;
}

saveToFile(filename);
}

void MainWindow::onQuit()
{
if (ui->textEdit->toPlainText() != m_loadedContent) {
if (QMessageBox::question(this, tr("Not saved"), tr("Document not saved, sure you want to quit?")) != QMessageBox::Yes) {
return;
}
}
close();
}
@@ -25,9 +25,20 @@ class MainWindow : public QMainWindow
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();

void loadFile(const QString &filename);
void saveToFile(const QString &filename);

private slots:
void open();
void save();
void saveAs();
void onQuit();

private:
Ui::MainWindow *ui;
QString m_loadedContent;
QString m_filename;
};

0 comments on commit d3d480d

Please sign in to comment.
You can’t perform that action at this time.