/*
* Copyright (C) 2020 Tianjin KYLIN Information Technology Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/&gt;.
*
*/

#include <QBitmap>
#include <QPalette>
#include <QPainter>
#include <QTextList>
//#include <QClipboard>
#include <QDebug>
#include <QFileDialog>
#include <QImageReader>

#include <KWindowSystem>
// Necessary for closeApplication()
#include <NETWM>

#include "widget.h"
#include "ui_widget.h"
#include "editPage.h"
#include "ui_editPage.h"
#include "windowmanage.hpp"
#include "information_collector.h"


EditPage::EditPage(Widget *page, int noteId, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Edit_page),
    m_editColor(0, 0, 0),
    m_noteId(noteId),
    m_lastBlockList(0),
    m_notebook(page),
    m_setSizePage(new SetFontSize(m_notebook)),
    m_setColorFontPage(new SetFontColor(m_notebook)),
    m_isFullscreen(false),
    m_isInsImg(false)
{
    ui->setupUi(this);
    initSetup();
    slotsSetup();
    installEventFilter(this);

    setFixedSize(390, 364);
}

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

void EditPage::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    QPainterPath rectPath;
    rectPath.addRect(this->rect());
    p.fillPath(rectPath, palette().color(QPalette::Base));
}

bool EditPage::eventFilter(QObject *obj,QEvent *event)
{
    if (obj == this) {
        if (event->type() == QEvent::WindowActivate) {
            m_noteHead->hide();
            m_noteHeadMenu->show();
        }
        if (event->type() == QEvent::WindowDeactivate) {
            m_noteHead->show();
            m_noteHeadMenu->hide();
        }
    }
    return false;
}

void EditPage::initSetup()
{
    // 标题
    this->setWindowTitle(tr("Notes"));


#if (QT_VERSION >= QT_VERSION_CHECK(5, 12, 0))
    // 高分屏适配
    QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif

    kabase::WindowManage::removeHeader(this);
    // 配置按钮
    initColor();
    // 获取字体按钮状态
    ui->fontTwinButtonGroup->getFontSizeBtn()->setButtonSize("11");
    fontChanged(ui->textEdit->font());

    m_noteHead = new noteHead(this);
    m_noteHeadMenu = new noteHeadMenu(this);
    m_colorPage = new SelectColor(m_notebook, m_noteHeadMenu->ui->pushButtonPalette);

    ui->verticalLayout_3->addWidget(m_noteHead);
    ui->verticalLayout_3->addWidget(m_noteHeadMenu);
    m_noteHeadMenu->hide();

    ui->textEdit->setFrameShape(QFrame::NoFrame);
    ui->textEdit->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    // 右键菜单
    ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->textEdit, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(textRightMenu(QPoint)));

    // 设置字体大小
    m_setSizePage = new SetFontSize(ui->fontTwinButtonGroup->getFontSizeBtn());
    m_setSizePage->hide();
    // 设置字体颜色
    m_setColorFontPage = new SetFontColor(this);
    m_setColorFontPage->hide();
}


void EditPage::slotsSetup()
{
    connect(m_noteHeadMenu->ui->pushButtonExit, &QPushButton::clicked, this, [=](){
        if (ui->textEdit->document()->isEmpty()) {
            emit isEmptyNote(m_noteId);
        }
        this->close();
    });
    connect(m_noteHeadMenu->m_menuActionDel, &QAction::triggered, this, [=](){
        this->close();
        requestDel(m_noteId);
    });
    connect(ui->textEdit, &QTextEdit::textChanged, this, &EditPage::textChangedSlot);
    // 调色板菜单
    connect(m_noteHeadMenu->ui->pushButtonPalette, &QPushButton::clicked, this, [=](){
        m_colorPage->show();
    });
    // 调色按钮
    connect(m_colorPage->paletteWidget->ui->blueBtn, &QPushButton::clicked, this,
            &EditPage::blueBtnSlot);
    connect(m_colorPage->paletteWidget->ui->redBtn, &QPushButton::clicked, this,
            &EditPage::redBtnSlot);
    connect(m_colorPage->paletteWidget->ui->darkGreenBtn, &QPushButton::clicked, this,
            &EditPage::darkGreenBtnSlot);
    connect(m_colorPage->paletteWidget->ui->orangeBtn, &QPushButton::clicked, this,
            &EditPage::orangeBtnSlot);
    connect(m_colorPage->paletteWidget->ui->purpleBtn, &QPushButton::clicked, this,
            &EditPage::purpleBtnSlot);
    connect(m_colorPage->paletteWidget->ui->goldenBtn, &QPushButton::clicked, this,
            &EditPage::goldenBtnSlot);
    connect(m_colorPage->paletteWidget->ui->greyBtn, &QPushButton::clicked, this,
            &EditPage::greyBtnSlot);
    connect(m_colorPage->paletteWidget->ui->pinkBtn, &QPushButton::clicked, this,
            &EditPage::pinkBtnSlot);
    // 字体颜色大小
    connect(m_setSizePage->ui->listWidget, &QListWidget::itemClicked, this,
            &EditPage::setFontSizeSlot);
    connect(m_setColorFontPage->ui->listWidget, &QListWidget::itemClicked, this,
            &EditPage::setFontColorSlot);
    // 文本格式
    connect(ui->fontPropertyWidget->boldBtn(), &QPushButton::clicked, this, &EditPage::setBoldSlot);
    connect(ui->fontPropertyWidget->italicBtn(), &QPushButton::clicked, this, &EditPage::setItalicSlot);
    connect(ui->fontPropertyWidget->underlineBtn(), &QPushButton::clicked, this, &EditPage::setUnderlineSlot);
    connect(ui->fontPropertyWidget->strikeOutBtn(), &QPushButton::clicked, this, &EditPage::setStrikeOutSlot);
    connect(ui->fontPropertyWidget->unorderedBtn(), &QPushButton::clicked, this, &EditPage::setUnorderedListSlot);
    connect(ui->fontPropertyWidget->orderedBtn(), &QPushButton::clicked, this, &EditPage::setOrderedListSlot);
    connect(ui->fontPropertyWidget->insertBtn(), &QPushButton::clicked, this, &EditPage::insertpicture);
    connect(ui->textEdit, &QTextEdit::cursorPositionChanged, this,
            &EditPage::cursorPositionChangedSlot);
    connect(ui->textEdit, &QTextEdit::currentCharFormatChanged, this,
            &EditPage::currentCharFormatChangedSlot);

    connect(ui->fontTwinButtonGroup->getFontSizeBtn(), &QPushButton::clicked, this, [=](){
        m_setSizePage->show();
    });
    connect(ui->fontTwinButtonGroup->getFontColorBtn(), &QPushButton::clicked, this, &EditPage::onFontColorClicked);
    connect(m_noteHeadMenu, &noteHeadMenu::requestUpdateMenuIcon, this, [=](){
        KWindowInfo info(this->winId(), NET::WMState);
        bool b = info.state() & NET::KeepAbove;

        if(b){
            m_noteHeadMenu->m_topAction->setIcon(QPixmap(":/image/1x/select.png"));
        }
        else{
            m_noteHeadMenu->m_topAction->setIcon(QPixmap(""));
        }
    });


    connect(m_noteHeadMenu, &noteHeadMenu::requestUpdateMenuIcon, this, [=](){
        KWindowInfo info(this->winId(), NET::WMState);
        bool b = info.state() & NET::KeepAbove;

        if(b){
            m_noteHeadMenu->m_topAction->setIcon(QPixmap(":/image/1x/select.png"));
        }
        else{
            m_noteHeadMenu->m_topAction->setIcon(QPixmap(""));
        }
    });

    connect(m_noteHeadMenu, &noteHeadMenu::requestTopMost, this, [=](){
        InformationCollector::getInstance().addPoint(InformationCollector::UiTop);
         KWindowInfo info(this->winId(), NET::WMState);
         bool b = info.state() & NET::KeepAbove;

         if(b) {
             setWindowStatusClear();
         }
         else {
             setWindowKeepAbove();
         }
     });

    connect(&UkuiSettingsMonitor::instance(), &UkuiSettingsMonitor::tabletModeUpdate,
            this, &EditPage::handleTabletMode);
}

void EditPage::mergeFormatOnWordOrSelection(const QTextCharFormat &format)
{
    QTextCursor cursor = ui->textEdit->textCursor();
    if (!cursor.hasSelection()) {
        // cursor.select(QTextCursor::WordUnderCursor);
    }
    cursor.mergeCharFormat(format);
    ui->textEdit->mergeCurrentCharFormat(format);
    ui->textEdit->setFocus(Qt::TabFocusReason);
}

void EditPage::fontChanged(const QFont &f)
{
    qDebug() << "font Changed" << f.pointSize() << f.bold();
    m_setSizePage->ui->listWidget->setCurrentRow(f.pointSize() - 10);
    ui->fontTwinButtonGroup->getFontSizeBtn()->setButtonSize(QString::number(f.pointSize()));
    if(f.pointSize() < 10 )
    {
        ui->fontTwinButtonGroup->getFontSizeBtn()->setButtonSize(QString::number(10));
    }
    else
    {
        ui->fontTwinButtonGroup->getFontSizeBtn()->setButtonSize(QString::number(f.pointSize()));
    }
    ui->fontPropertyWidget->boldBtn()->setChecked(f.bold());
    ui->fontPropertyWidget->italicBtn()->setChecked(f.italic());
    ui->fontPropertyWidget->underlineBtn()->setChecked(f.underline());
    ui->fontPropertyWidget->strikeOutBtn()->setChecked(f.strikeOut());

    if (ui->textEdit->textCursor().currentList()) {
        QTextListFormat lfmt = ui->textEdit->textCursor().currentList()->format();
        if (lfmt.style() == QTextListFormat::ListDisc) {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(true);
            ui->fontPropertyWidget->orderedBtn()->setChecked(false);
        } else if (lfmt.style() == QTextListFormat::ListDecimal) {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
            ui->fontPropertyWidget->orderedBtn()->setChecked(true);
        } else {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
            ui->fontPropertyWidget->orderedBtn()->setChecked(false);
        }
    } else {
        ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
        ui->fontPropertyWidget->orderedBtn()->setChecked(false);
    }
}

void EditPage::fontColorChanged(const QColor &c)
{
    ui->fontTwinButtonGroup->getFontColorBtn()->setButtonColor(c);
}

void EditPage::list(bool checked, QTextListFormat::Style style)
{
    QTextCursor cursor = ui->textEdit->textCursor();
    cursor.beginEditBlock();
    if (!checked) {
        qDebug() << "unchecked";
        QTextBlockFormat obfmt = cursor.blockFormat();
        QTextBlockFormat bfmt;
        bfmt.setIndent(obfmt.indent());
        cursor.setBlockFormat(bfmt);
//        QTextDocument *document = ui->textEdit->document();
//        document->setIndentWidth(0);
    } else {
        qDebug() << "checked";
        QTextCharFormat charFormat = cursor.charFormat();
        qDebug() << charFormat.fontPointSize() << cursor.selectedText() ;
        qDebug() << cursor.anchor() << cursor.position();
        QTextListFormat listFmt;
        if (cursor.currentList()) {
            listFmt = cursor.currentList()->format();
        }
        listFmt.setStyle(style);
//        QTextDocument *document = ui->textEdit->document();
//        document->setIndentWidth(15);
        cursor.createList(listFmt);
    }
    cursor.endEditBlock();
}


/**************** Slots *******************/

void EditPage::cursorPositionChangedSlot()
{
    qDebug() << "cursorPositionChangedSlot";
    QTextList *l = ui->textEdit->textCursor().currentList();
    if (m_lastBlockList && (l == m_lastBlockList || (l != 0 && m_lastBlockList != 0
                                                     && l->format().style()
                                                     == m_lastBlockList->format().style()))) {
        return;
    }
    m_lastBlockList = l;
    if (l) {
        QTextListFormat lfmt = l->format();
        if (lfmt.style() == QTextListFormat::ListDisc) {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(true);
            ui->fontPropertyWidget->orderedBtn()->setChecked(false);
        } else if (lfmt.style() == QTextListFormat::ListDecimal) {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
            ui->fontPropertyWidget->orderedBtn()->setChecked(true);
        } else {
            ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
            ui->fontPropertyWidget->orderedBtn()->setChecked(false);
        }
    } else {
        ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
        ui->fontPropertyWidget->orderedBtn()->setChecked(false);
    }
}

void EditPage::currentCharFormatChangedSlot(const QTextCharFormat &format)
{
    qDebug() << "currentCharFormatChangedSlot";
    fontChanged(format.font());
    // fontColorChanged((format.foreground().isOpaque()) ? format.foreground().color() : QColor());
    fontColorChanged(format.foreground().color());
}

void EditPage::textChangedSlot()
{
    qDebug() << "emit textchange" << "note id" << m_noteId << this->m_id;
    emit texthasChanged(m_noteId, this->m_id);
}

//textedit右键菜单
void EditPage::textRightMenu(QPoint)
{
    QMenu *m_rightMenu = new QMenu;

    //定义action
    QAction *undoact = new QAction(tr("undo"));
    QAction *redoact = new QAction(tr("redo"));
    QAction *cutact = new QAction(tr("cut"));
    QAction *copyact = new QAction(tr("copy"));
    QAction *pasteact = new QAction(tr("paste"));
    QAction *textfornew = new QAction(tr("copy to newpage"));

    undoact->setIcon(QIcon::fromTheme("edit-undo"));
    redoact->setIcon(QIcon::fromTheme("edit-redo"));
    cutact->setIcon(QIcon::fromTheme("edit-cut"));
    copyact->setIcon(QIcon::fromTheme("edit-copy"));
    pasteact->setIcon(QIcon::fromTheme("edit-paste"));
//    textfornew->setIcon(QIcon::fromTheme("add-files-to-archive"));
    textfornew->setIcon(QIcon(":/image/1x/copy_to_new_note.png"));
    //定义action end

    //action使能与非使能
    connect(ui->textEdit->document(), SIGNAL(undoAvailable(bool)),
            undoact, SLOT(setEnabled(bool)));
    connect(ui->textEdit->document(), SIGNAL(redoAvailable(bool)),
            redoact, SLOT(setEnabled(bool)));
    undoact->setEnabled(ui->textEdit->document()->isUndoAvailable());
    redoact->setEnabled(ui->textEdit->document()->isRedoAvailable());

    cutact->setEnabled(!ui->textEdit->textCursor().selectedText().isEmpty());
    copyact->setEnabled(!ui->textEdit->textCursor().selectedText().isEmpty());
    pasteact->setEnabled(!QApplication::clipboard()->text().isEmpty());
    connect(QApplication::clipboard(), &QClipboard::dataChanged, this, [=](){
        pasteact->setEnabled(!QApplication::clipboard()->text().isEmpty());
    });

    textfornew->setEnabled(!ui->textEdit->textCursor().selectedText().isEmpty());
    //action使能与非使能 end

    //action绑定槽函数
    connect(undoact, SIGNAL(triggered(bool)), ui->textEdit, SLOT(undo()));
    connect(redoact, SIGNAL(triggered(bool)), ui->textEdit, SLOT(redo()));

    connect(cutact, SIGNAL(triggered()), ui->textEdit, SLOT(cut()));
    connect(copyact, SIGNAL(triggered()), ui->textEdit, SLOT(copy()));
    connect(pasteact, SIGNAL(triggered(bool)), ui->textEdit, SLOT(paste()));

    connect(textfornew, SIGNAL(triggered()), this, SLOT(textForNewEditpageSlot()));
    //action绑定槽函数 end

    //action添加到menu
    m_rightMenu->addAction(undoact);
    m_rightMenu->addAction(redoact);
    m_rightMenu->addSeparator();         // 添加分割线

    m_rightMenu->addAction(cutact);
    m_rightMenu->addAction(copyact);
    m_rightMenu->addAction(pasteact);
    // m_rightMenu->addSeparator();         // 添加分割线

    // m_rightMenu->addAction(textfornew);
    //action添加到menu end

    m_rightMenu->move (cursor().pos());
    m_rightMenu->show();

}

//拷贝选中内容到新便签页
void EditPage::textForNewEditpageSlot()
{
    if(!ui->textEdit->textCursor().selectedText().isEmpty())
    {
        ui->textEdit->copy();
        emit textForNewEditpage();
    }
}

// 加粗
void EditPage::setBoldSlot()
{
    InformationCollector::getInstance().addPoint(InformationCollector::Bold);
    qDebug() << "setBoldSlot";
    QTextCharFormat fmt;
    fmt.setFontWeight(ui->fontPropertyWidget->boldBtn()->isCheckable() ? QFont::Bold : QFont::Normal);

    QTextCursor cursor = ui->textEdit->textCursor();
    if (cursor.charFormat().fontWeight() == QFont::Bold) {
        qDebug() << "current cursor charFormat QFont::Bold";
        fmt.setFontWeight(QFont::Normal);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    } else {
        qDebug() << "current cursor charFormat QFont::Normal";
        fmt.setFontWeight(QFont::Bold);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    }
}

// 斜体
void EditPage::setItalicSlot()
{
    InformationCollector::getInstance().addPoint(InformationCollector::Italics);
    qDebug()<<"-------setItalicSlot------------";
    QTextCharFormat fmt;
    fmt.setFontItalic(ui->fontPropertyWidget->italicBtn()->isCheckable());// ? QFont::StyleItalic : QFont::Normal);
    // mergeFormatOnWordOrSelection(fmt);

    QTextCursor cursor = ui->textEdit->textCursor();
    if (cursor.charFormat().fontItalic()) { // return boolProperty(FontItalic)
        fmt.setFontItalic(QFont::StyleNormal);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    } else {
        fmt.setFontItalic(QFont::StyleItalic);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    }
}

// 划线
void EditPage::setUnderlineSlot()
{
    InformationCollector::getInstance().addPoint(InformationCollector::Underline);
    qDebug() << "setUnderlineSlot";
    QTextCharFormat fmt;
    fmt.setFontUnderline(ui->fontPropertyWidget->underlineBtn()->isCheckable());// ? QFont::UnderlineResolved : QFont::Normal );
    // mergeFormatOnWordOrSelection(fmt);

    QTextCursor cursor = ui->textEdit->textCursor();
    if (cursor.charFormat().fontUnderline()) { //
        fmt.setFontUnderline(QFont::StyleNormal);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    } else {
        ui->textEdit->mergeCurrentCharFormat(fmt);
    }
}

// 除线
void EditPage::setStrikeOutSlot()
{
    InformationCollector::getInstance().addPoint(InformationCollector::Deleteline);
    qDebug() << "setStrikeOutSlot";
    QTextCharFormat fmt;
    fmt.setFontStrikeOut(ui->fontPropertyWidget->strikeOutBtn()->isCheckable());// ? QFont::StrikeOutResolved : QFont::Normal );
    // mergeFormatOnWordOrSelection(fmt);

    QTextCursor cursor = ui->textEdit->textCursor();
    if (cursor.charFormat().fontStrikeOut()) {
        fmt.setFontStrikeOut(QFont::StyleNormal);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    } else {
        ui->textEdit->mergeCurrentCharFormat(fmt);
    }
}

// 无序列表
void EditPage::setUnorderedListSlot(bool checked)
{
    qDebug() << "setUnorderedListSlot" << checked;
    if (checked) {
        InformationCollector::getInstance().addPoint(InformationCollector::UnorderList);
        ui->fontPropertyWidget->orderedBtn()->setChecked(false);
        qDebug() << "show list set false" << checked;
        qDebug() << "text_edit_page->ui->fontPropertyWidget->orderedBtn() is checked ? :" << ui->fontPropertyWidget->orderedBtn()->isChecked();
    }
    list(checked, QTextListFormat::ListDisc);
    qDebug() << "text_edit_page->ui->fontPropertyWidget->orderedBtn() is checked ? :" << ui->fontPropertyWidget->orderedBtn()->isChecked();
}

// 序列表
void EditPage::setOrderedListSlot(bool checked)
{
    qDebug() << "show num list";
    if (checked) {
        InformationCollector::getInstance().addPoint(InformationCollector::OrderList);
        ui->fontPropertyWidget->unorderedBtn()->setChecked(false);
    }
    list(checked, QTextListFormat::ListDecimal);
}

// 字号
void EditPage::setFontSizeSlot()
{
    qDebug() << "setFontSizeSlot";
    int num = m_setSizePage->ui->listWidget->currentRow();
    ui->fontTwinButtonGroup->getFontSizeBtn()->setButtonSize(QString::number(num+10));
    m_setSizePage->close();
    update();
    QTextCharFormat fmt;
    InformationCollector::getInstance().addMessage(QString("set font size to %1.").arg(num+10));
    fmt.setFontPointSize(num+10);
    mergeFormatOnWordOrSelection(fmt);
}

// 调色板
void EditPage::initColor()
{
    m_colorNum[0] = QColor(SetFontColor::KY_BLUE);
    m_colorNum[1] = QColor(SetFontColor::KY_RED);
    m_colorNum[2] = QColor(SetFontColor::KY_GREEN);
    m_colorNum[3] = QColor(SetFontColor::KY_ORANGE);
    m_colorNum[4] = QColor(SetFontColor::KY_PURPLE);
}

void EditPage::setFontColorSlot(QListWidgetItem *item)
{
    qDebug() << "Sender" << sender();
    qDebug() << "Item" << item;
    int num = m_setColorFontPage->ui->listWidget->currentRow();
    QTextCharFormat fmt;
    if (num != 5) {
        InformationCollector::getInstance().addMessage(QString("set font color to %1.").arg(m_colorNum[num].name()));
        fmt.setForeground(m_colorNum[num]);
        ui->textEdit->mergeCurrentCharFormat(fmt);
    } else {
        fmt.setForeground(palette().color(QPalette::Text));
        // fmt.clearForeground();
        ui->textEdit->mergeCurrentCharFormat(fmt);
    }
    m_setColorFontPage->close();
}

void EditPage::blueBtnSlot()
{
    QColor color((PaletteWidget::KY_BLUE));
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::redBtnSlot()
{
    QColor color((PaletteWidget::KY_RED));
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::darkGreenBtnSlot()
{
    QColor color(PaletteWidget::KY_GREEN);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::orangeBtnSlot()
{
    QColor color(PaletteWidget::KY_ORANGE);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::purpleBtnSlot()
{
    QColor color(PaletteWidget::KY_PURPLE);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::goldenBtnSlot()
{
    QColor color(PaletteWidget::KY_YELLOW);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::greyBtnSlot()
{
    QColor color(PaletteWidget::KY_GREY);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::pinkBtnSlot()
{
    QColor color(PaletteWidget::KY_PINK);
    m_editColor = color;
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = color;
    m_noteHeadMenu->colorWidget = color;
    update();
}

void EditPage::showFullScreenSlot()
{
    if (!m_isFullscreen) {
        this->showFullScreen();
        m_isFullscreen = true;
    } else {
        this->showNormal();
        m_isFullscreen = false;
    }
}

void EditPage::setWindowKeepAbove()
{
    KWindowSystem::setState(this->winId(), NET::KeepAbove);
}

void EditPage::setWindowStatusClear()
{
    KWindowSystem::clearState(this->winId(), NET::KeepAbove);
}

void EditPage::dropImage(const QImage& image, const QString& format) {
    QByteArray bytes;
    QBuffer buffer(&bytes);
    buffer.open(QIODevice::WriteOnly);
    image.save(&buffer, format.toLocal8Bit().data());
    buffer.close();
    QByteArray base64 = bytes.toBase64();
    QByteArray base64l;
    for (int i=0; i<base64.size(); i++) 
    {
        base64l.append(base64[i]);
        if (i%80 == 0) 
        {
            base64l.append("\n");
        }
    }

    if(image.isNull())
    {
        return ;
    }

    QTextCursor cursor = ui->textEdit->textCursor();

    if(cursor.atStart())
    {
        m_isInsImg = true;
    }

    QTextImageFormat imageFormat;
    imageFormat.setWidth  ( image.width() );
    imageFormat.setHeight ( image.height() );
    imageFormat.setName   ( QString("data:image/%1;base64,%2")
                                .arg(QString("%1.%2").arg(rand()).arg(format))
                                .arg(base64l.data())
                                );
    cursor.insertImage    ( imageFormat );
}


void EditPage::insertpicture()
{
    InformationCollector::getInstance().addPoint(InformationCollector::InsertPicture);
        QSettings s;
        QString attdir = s.value("general/filedialog-path").toString();
        QString file = QFileDialog::getOpenFileName(this,
                                        tr("Select an image"),
                                        attdir,
                                        tr("JPEG (*.jpg);; GIF (*.gif);; PNG (*.png);; BMP (*.bmp);; All (*)"));
        QImage image = QImageReader(file).read();
        dropImage(image, QFileInfo(file).suffix().toUpper().toLocal8Bit().data());
}

void EditPage::setHints()
{
    kabase::WindowManage::removeHeader(this);
}

void EditPage::defaultTextColorSlot()
{
    QPalette pal;
    m_editColor = pal.text().color();
    emit colorhasChanged(m_editColor, m_noteId);
    m_noteHead->colorWidget = m_editColor;
    m_noteHeadMenu->colorWidget = m_editColor;
    update();
}

void EditPage::onFontColorClicked()
{
    QPointF position = this->pos();
    QSize size = this->size();
    qDebug () << "Current size:" << size;
    m_setColorFontPage->move(position.x()+size.width() - 168 - 8 , position.y() + size.height() - 70);
    m_setColorFontPage->show();
}

void EditPage::enableMaximum()
{
    setMaximumSize(QWIDGETSIZE_MAX,QWIDGETSIZE_MAX);
}

void EditPage::disableMaximum()
{
    setMaximumSize(this->minimumSize());
}

void EditPage::handleTabletMode(UkuiSettingsMonitor::TabletStatus status)
{
    switch (status) {
    case UkuiSettingsMonitor::TabletStatus::PCMode:
        handlePCMode();
        break;
    case UkuiSettingsMonitor::TabletStatus::PADHorizontalMode:
    case UkuiSettingsMonitor::TabletStatus::PADVerticalMode:
        handlePADMode();
        break;
    default:
        break;
    }
}

void EditPage::handlePADMode()
{
    enableMaximum();
    showMaximized();
}

void EditPage::handlePCMode()
{
    disableMaximum();
}

void EditPage::showEvent(QShowEvent *event)
{
    handleTabletMode(UkuiSettingsMonitor::instance().tabletMode());
    QWidget::showEvent(event);
}