#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
init();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::init()
{
color = 0; // 色彩样式为1
start_x = 0; // 文章从第1列开始绘制
start_y = 0; // 文章从第1行开始绘制
choice_x = -1; // 用于标记选择开始的列
choice_y = -1; // 用于标记选择开始的行
cursor_x = 0; // 光标在第1列
cursor_y = 0; // 光标在第1行
clip = QApplication::clipboard();
cursor_show = true; // 光标处于显示的状态
ctrl_down = false; // alt按键没有按下
// 创建定时器
cursor_timer = new QTimer(this);
// 启动定时器
cursor_timer->start(CURSOR_TIME);
// 执行内容
connect(cursor_timer, &QTimer::timeout, [=](){
cursor_show = !cursor_show;
update();
});
// 初始化允许输入法
setAttribute(Qt::WA_InputMethodEnabled, true);
// 初始化底部提示栏
statusbar = statusBar();
setStatusBar(statusbar);
QString str = "页面: 0-0 当前: 0 总: 0";
statusbar_label = new QLabel(str, this);
article.append("欢迎使用此文本编辑器!");
article.append("按住alt可实现选择文本,配合滚轮实现左右滑动");
calculationGrid();
modifyStatusBarText();
}
void MainWindow::calculationGrid()
{
x_max = 0; // 屏幕最多绘制多少列
while(x_max * X_SPACE + X_ADD < width()) {
x_max++;
}
x_max = x_max - 1;
y_max = 0; // 屏幕最多绘制多少行
while(y_max * Y_SPACE + Y_ADD < height()) {
y_max++;
}
y_max = y_max - 1;
}
void MainWindow::modifyStatusBarText()
{
QString str;
if(start_y + y_max > article.size()) {
str = "页面: " + (QString::number(start_y + 1)) + "-" + (QString::number(article.size())) +
" 总: " + (QString::number(article.size())) + " 当前:" + (QString::number(cursor_y + 1));
}
else {
str = "页面: " + (QString::number(start_y + 1)) + "-" + (QString::number(start_y + y_max)) +
" 总: " + (QString::number(article.size())) + " 当前:" + (QString::number(cursor_y + 1));
}
delete statusbar_label;
statusbar_label = new QLabel(str, this);
statusbar->addWidget(statusbar_label);
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
int x = (event->x()) / X_SPACE + start_x;
int y = (event->y() - 20) / Y_SPACE + start_y;
int list_len = article.length();
if(y > list_len - 1) {
y = list_len - 1;
}
int line_len = article[y].length();
if(x > line_len) {
x = line_len;
}
cursor_x = x;
cursor_y = y;
if(ctrl_down == false) {
choice_x = x;
choice_y = y;
}
}
update();
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if(ctrl_down == false) {
choice_x = -1;
choice_y = -1;
}
modifyStatusBarText();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
int _x = choice_x;
int _y = choice_y;
if(_x > -1 || _y > -1) {
int x = (event->x()) / X_SPACE + start_x;
int y = (event->y() - 20) / Y_SPACE + start_y;
int list_len = article.length();
if(y > list_len - 1) {
y = list_len - 1;
}
int line_len = article[y].length();
if(x > line_len) {
x = line_len;
}
cursor_x = x;
cursor_y = y;
update();
}
}
void MainWindow::wheelEvent(QWheelEvent *event)
{
QPoint p = event->angleDelta();
if(ctrl_down == false) {
if(p.y() > 0) {
if(start_y > 0) {
start_y -= 3;
if(start_y < 0) {
start_y = 0;
}
}
}
else if(p.y() < 0) {
start_y += 3;
if(start_y > article.length()) {
start_y = article.length() - 1;
}
}
modifyStatusBarText();
update();
}
else {
if(p.y() > 0) {
start_x += 3;
}
else if(p.y() < 0) {
if(start_x > 0) {
start_x -= 3;
if(start_x < 0) {
start_x = 0;
}
}
}
}
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
calculationGrid();
int key = event->key();
QString text = event->text();
QString line = article[cursor_y];
switch (key) {
case Qt::Key_Left:
if(cursor_x > 0) {
cursor_x = cursor_x - 1;
if(cursor_x < start_x) {
start_x = start_x - 1;
}
modifyStatusBarText();
cursor_show = true;
update();
}
break;
case Qt::Key_Right:
if(cursor_x < line.length()) {
cursor_x = cursor_x + 1;
if(cursor_x > start_x + x_max) {
start_x = start_x + 1;
}
modifyStatusBarText();
cursor_show = true;
update();
}
break;
case Qt::Key_Up:
if(cursor_y > 0) {
if(cursor_x > article[cursor_y - 1].length()) {
cursor_x = article[cursor_y - 1].length();
}
cursor_y = cursor_y - 1;
if(cursor_y < start_y) {
start_y = start_y - 1;
}
modifyStatusBarText();
cursor_show = true;
update();
}
break;
case Qt::Key_Down:
if(cursor_y < article.length() - 1) {
if(cursor_x > article[cursor_y + 1].length()){
cursor_x = article[cursor_y + 1].length();
}
cursor_y = cursor_y + 1;
if(cursor_y > start_y + y_max - 1) {
start_y = start_y + 1;
}
modifyStatusBarText();
cursor_show = true;
update();
}
break;
case Qt::Key_Return:
{
QString right_str = line.right(line.length() - cursor_x);
article.insert(cursor_y + 1, right_str);
article[cursor_y] = line.left(cursor_x);
cursor_x = 0;
cursor_y = cursor_y + 1;
if(cursor_y > start_y + y_max - 1) {
start_y = start_y + 1;
}
modifyStatusBarText();
cursor_show = true;
update();
}
break;
case Qt::Key_Backspace:
if(cursor_x > 0) {
article[cursor_y] = line.left(cursor_x - 1) + line.right(line.length() - cursor_x);
cursor_x = cursor_x -1;
if(cursor_x < start_x + 2) {
start_x = start_x - 2;
if(start_x < 0) {
start_x = 0;
}
}
if(cursor_y < start_y) {
start_y = cursor_y;
}
modifyStatusBarText();
cursor_show = true;
update();
}
else {
if(cursor_y > 0) {
int src_len = article[cursor_y - 1].length();
article[cursor_y - 1] += line;
article.removeAt(cursor_y);
cursor_x = src_len;
cursor_y = cursor_y -1;
if(cursor_x > start_x + x_max) {
start_x = src_len - 5;
if(start_x < 0) {
start_x = 0;
}
}
if(cu
QT开发的一款的文本编辑器
需积分: 0 92 浏览量
2022-09-22
22:41:26
上传
评论
收藏 9KB ZIP 举报
herb.dr
- 粉丝: 168
- 资源: 7
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0