#include "codeedit.h"
#include <QDebug>
#include <QCompleter>
CodeEdit::CodeEdit(QWidget *parent): QPlainTextEdit(parent)
{
this->myFile=new QFile(this);
this->lineNumberArea=new LineNumberArea(this);
this->setFont(QFont(tr("Consolas"), 14));
this->setStyleSheet("background-color:rgb(33,33,33);color:rgb(240,180,40);border:0px;");
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highLightCurrentLine()));
connect(this,SIGNAL(cursorPositionChanged()),this,SLOT(showCompleteWidget()));
highLightCurrentLine();
setViewportMargins(40, 0, 0, 0);
QFontMetrics metrics(this->font());
this->setTabStopWidth(4 * metrics.width(' '));
setUpCompleteList();
completeWidget= new CompleteListWidget(this);
completeWidget->hide();
completeWidget->setMaximumHeight(14*5);
completeState=CompleteState::Hide;
}
void CodeEdit::lineNumberAreaPaintEvent(QPaintEvent *event)
{
//qDebug()<<"yes";
QPainter painter(lineNumberArea);
painter.fillRect(QRect(0,0,40,1560),QColor(34,39,49)); //画一个灰色的矩形底色
QTextBlock block=firstVisibleBlock(); //获取第一个可见文本块
int num=block.blockNumber(); //获取第一个行号
int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
//这个过程可能难以理解,实际上就是对于某一行代码块建立逻辑坐标系,第一行代码块左上角是(0,0),建议回顾Qt坐标系统
//qDebug()<<blockBoundingGeometry(block).translated(contentOffset());不理解的画建议用QDebug输出一下它的返回值,看看它代表什么含义
int bottom = top + (int) blockBoundingRect(block).height(); //就是代码块矩形底下的线,这里换算为逻辑纵坐标,也就是加上一行代码块的高度
//qDebug()<<(int) blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) { //当这个代码块合法且代码块矩形上面线坐标小于等于下面线坐标,当文档结束时,则不符合这两个条件,想想为什么
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(num + 1); //画出行号
painter.setPen(Qt::lightGray);
painter.drawText(0, top, 40, fontMetrics().height(),
Qt::AlignCenter, number);
}
block = block.next(); //代码块向后移一行
top = bottom; //同样当前坐标更新
//qDebug()<<top;
bottom = top + (int) blockBoundingRect(block).height();
++num;
}
}
void CodeEdit::highLightCurrentLine()
{
QList<QTextEdit::ExtraSelection> extraSelections;
if (!isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor(66,66,66);
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
}
setExtraSelections(extraSelections);
}
void CodeEdit::showCompleteWidget()
{
if(completeState==CompleteState::Ignore)return;//忽略光标和文本变化的响应,避免陷入事件死循环和互相钳制
completeWidget->hide();
completeState=CompleteState::Hide;
QString word=this->getWordOfCursor();
completeWidget->clear();
if(!word.isEmpty()){//光标所在单词是不是合法(能不能联想)
int maxSize=0;
QMap<QString,int> distance;
vector<QString> itemList;
foreach(const QString &temp,completeList){
if(temp.contains(word)){
//completeWidget->addItem(new QListWidgetItem(temp));
itemList.push_back(temp);
distance[temp]=CompleteListWidget::ldistance(temp.toStdString(),word.toStdString());
if(temp.length()>maxSize)maxSize=temp.length();
}
}
//有没有匹配的字符
if(itemList.size()>0){//如果有的话
sort(itemList.begin(),itemList.end(),[&](const QString &s1,const QString &s2)->bool{return distance[s1]<distance[s2]; });
foreach(const QString& item,itemList){
QListWidgetItem *pItem=new QListWidgetItem(item);
pItem->setFont(QFont(tr("Consolas"), 14));
//pItem->setSizeHint(QSize(70, 24)); //每次改变Item的高度
completeWidget->addItem(pItem);
}
int x=this->getCompleteWidgetX();
int y=this->cursorRect().y()+fontMetrics().height();
completeWidget->move(x,y);
if(completeWidget->count()>5)completeWidget->setFixedHeight(fontMetrics().height()*6);
else completeWidget->setFixedHeight(fontMetrics().height()*(completeWidget->count()+1));
completeWidget->setFixedWidth((fontMetrics().width(QLatin1Char('9'))+6)*maxSize);
completeWidget->show();
completeState=CompleteState::Showing;
completeWidget->setCurrentRow(0,QItemSelectionModel::Select);
}
}
}
int CodeEdit::lineNumberAreaWidth()
{
return 40;
}
void CodeEdit::setUpCompleteList()
{
completeList<< "char" << "class" << "const"
<< "double" << "enum" << "explicit"
<< "friend" << "inline" << "int"
<< "long" << "namespace" << "operator"
<< "private" << "protected" << "public"
<< "short" << "signals" << "signed"
<< "slots" << "static" << "struct"
<< "template" << "typedef" << "typename"
<< "union" << "unsigned" << "virtual"
<< "void" << "volatile" << "bool"<<"using"<<"constexpr"
<<"sizeof"<<"if"<<"for"<<"foreach"<<"while"<<"do"<<"case"
<<"break"<<"continue"<<"template"<<"delete"<<"new"
<<"default"<<"try"<<"return"<<"throw"<<"catch"<<"goto"<<"else"
<<"extren"<<"this"<<"switch"<<"#include <>"<<"#include \"\""<<"#define"<<"iostream";
}
void CodeEdit::resizeEvent(QResizeEvent *event)
{
QPlainTextEdit::resizeEvent(event);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), 40, cr.height()));
}
void CodeEdit::keyPressEvent(QKeyEvent *event)
{
//qDebug()<<event->key();
if(event->modifiers()==Qt::ShiftModifier&&event->key()==40){
this->insertPlainText(tr("()"));
this->moveCursor(QTextCursor::PreviousCharacter);
}
else if(event->modifiers()==Qt::ShiftModifier&&event->key()==34){
this->insertPlainText(tr("\"\""));
this->moveCursor(QTextCursor::PreviousCharacter);
}
else if(event->key()==16777235&&completeState==CompleteState::Showing){
if(completeWidget->currentRow()>0)
completeWidget->setCurrentRow(completeWidget->currentRow()-1);
}
else if(event->key()==16777237&&(completeState==CompleteState::Showing)){
if(completeWidget->currentRow()<completeWidget->count()-1)
completeWidget->setCurrentRow(completeWidget->currentRow()+1);
}
else if(event->key()==Qt::Key_Return&&(completeState==CompleteState::Showing)){
QString insertText=completeWidget->currentItem()->text();
QString word=this->getWordOfCursor();
completeState=CompleteState::Ignore;
for(int i=0;i<word.count();++i)
this->textCursor().deletePreviousChar();
this->insertPlainText(insertText);
if(insertText.contains(tr("#include")))
this->moveCursor(QTextCursor::PreviousCharacter);
completeState=CompleteState::Hide;
completeWidget->hide();
}//*
else if(event->key()==Qt::Key_Return){//回车下行层级自动
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备
资源推荐
资源详情
资源评论
收起资源包目录
C++寒假项目.zip (25个子文件)
cm
QtCode.pro 1KB
mainwindow.h 2KB
codeedit.h 1KB
stylesheet.qss 760B
mainwindow.cpp 7KB
completelistwidget.h 616B
main.cpp 172B
mainwindow.ui 534B
codeedit.cpp 11KB
highlighter.h 964B
img
search_1.png 5KB
file_1.png 3KB
file.png 2KB
logo.png 2KB
bug.png 5KB
search.png 3KB
app.png 2KB
git.png 2KB
bug_1.png 6KB
git_1.png 3KB
app_1.png 3KB
higherlighter.cpp.autosave 2KB
highlighter.cpp 4KB
res.qrc 475B
completelistwidget.cpp 2KB
共 25 条
- 1
资源评论
妄北y
- 粉丝: 1w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功