|
| 1 | +/* |
| 2 | + This file is part of cPad Project. |
| 3 | +
|
| 4 | + cPad is free software: you can redistribute it and/or modify |
| 5 | + it under the terms of the GNU General Public License as published by |
| 6 | + the Free Software Foundation, either version 3 of the License, or |
| 7 | + (at your option) any later version. |
| 8 | +
|
| 9 | + cPad is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with cPad. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +*/ |
| 17 | +#include <QtGui> |
| 18 | +#include "autocomple.h" |
| 19 | + |
| 20 | +AutoComple::AutoComple(CodeEditor *editor) |
| 21 | +{ |
| 22 | + codeEditor = editor; |
| 23 | + memoryCenter = new MemoryCenter; |
| 24 | + setGeometry(100, 100, 200, 60); |
| 25 | + setWindowFlags(Qt::Popup); |
| 26 | + //setWindowModality(Qt::WindowModal); |
| 27 | + setFocus(Qt::MouseFocusReason); |
| 28 | + setFont(codeEditor->font()); |
| 29 | + connect(this, SIGNAL(itemActivated(QListWidgetItem*)), |
| 30 | + this, SLOT(compleString(QListWidgetItem*))); |
| 31 | + connect(this, SIGNAL(itemPressed(QListWidgetItem*)), |
| 32 | + this, SLOT(compleString(QListWidgetItem*))); |
| 33 | +} |
| 34 | + |
| 35 | +void AutoComple::compleString(QListWidgetItem *item) |
| 36 | +{ |
| 37 | + QString word( item->text() ); |
| 38 | + word.remove(0, matchLength); |
| 39 | + QTextCursor tcr( codeEditor->textCursor() ); |
| 40 | + tcr.insertText(word); |
| 41 | + codeEditor->setTextCursor(tcr); |
| 42 | + this->close(); |
| 43 | +} |
| 44 | + |
| 45 | +void AutoComple::doAutoComple(int mode) |
| 46 | +{ |
| 47 | + if (autoCompleCheck(mode)) { |
| 48 | + //show之前计算坐标 |
| 49 | + QTextCursor tcr(codeEditor->textCursor()); |
| 50 | + QPoint pp = codeEditor->getCompleXY(); |
| 51 | + setGeometry(pp.x(), pp.y(), this->width(), this->height()); |
| 52 | + show(); |
| 53 | + } |
| 54 | + else |
| 55 | + close(); |
| 56 | +} |
| 57 | + |
| 58 | +bool AutoComple::autoCompleCheck(int mode) |
| 59 | +{ |
| 60 | + QTextCursor tcr( codeEditor->textCursor() ); |
| 61 | + QTextBlock blk( tcr.block() ); |
| 62 | + QString halfLineText = blk.text(); |
| 63 | + int posA( blk.position() ), posB( tcr.position() ); |
| 64 | + int length( halfLineText.length() ); |
| 65 | + halfLineText.remove( posB-posA, length-(posB-posA) ); |
| 66 | + |
| 67 | + if (halfLineText.endsWith(" ") || halfLineText.isEmpty() ) |
| 68 | + return false; |
| 69 | + else { |
| 70 | + LineHeadPointer *lineHeadTemp; |
| 71 | + LexicalAnalysis *wordTemp; |
| 72 | + CompleList *findCompleResult; |
| 73 | + QString wordString; |
| 74 | + //matchNumber = matchLength = 0; |
| 75 | + //matchNumber是返回的链表的长度;matchLength是当前半词长度 |
| 76 | + //这里必须先保证最后一个字符不是空格 |
| 77 | + memoryCenter -> analysisMain ( halfLineText );//参数为当前行内容 |
| 78 | + lineHeadTemp = memoryCenter -> getLineHead ( ); |
| 79 | + for ( ; lineHeadTemp -> nextLine ; lineHeadTemp = lineHeadTemp -> nextLine ); |
| 80 | + for ( wordTemp = lineHeadTemp -> firstWord ; wordTemp -> nextWord ; wordTemp = wordTemp -> nextWord ); |
| 81 | + if ( wordTemp -> wordCategory == 92 || ( wordTemp -> wordCategory >= 50 && wordTemp -> wordCategory <= 89 ) ) |
| 82 | + { |
| 83 | + wordString = wordTemp -> wordContent; |
| 84 | + if (mode) { |
| 85 | + QString codeText( codeEditor->toPlainText() ); |
| 86 | + memoryCenter -> analysisMain ( codeText );//参数为全文内容 |
| 87 | + memoryCenter -> makeCompleList ( ); |
| 88 | + } |
| 89 | + findCompleResult = memoryCenter -> findCompleList ( wordString , matchNumber , matchLength ); |
| 90 | + } |
| 91 | + else |
| 92 | + findCompleResult = NULL; |
| 93 | + |
| 94 | + if (!findCompleResult) |
| 95 | + return false; |
| 96 | + |
| 97 | + this->clear();//清除原来的数据 |
| 98 | + QListWidgetItem *i; |
| 99 | + |
| 100 | + int maxLength = 0, tempLength; |
| 101 | + QString maxWord; |
| 102 | + for ( int j = matchNumber; j; j-- , findCompleResult = findCompleResult -> nextComple ) |
| 103 | + { |
| 104 | + i = new QListWidgetItem(); |
| 105 | + i->setText(findCompleResult -> compleName); |
| 106 | + tempLength = (i->text()).length(); |
| 107 | + if (tempLength > maxLength) { |
| 108 | + maxLength = tempLength; |
| 109 | + maxWord = i->text(); |
| 110 | + } |
| 111 | + i->setData(Qt::UserRole, findCompleResult -> compleNameLength); |
| 112 | + this->addItem(i); |
| 113 | + } |
| 114 | + this->setCurrentRow(0); |
| 115 | + /*if (matchNumber == 1) { |
| 116 | + emit itemActivated(item(0)); |
| 117 | + return false; |
| 118 | + }*/ |
| 119 | + if (matchNumber >= 5) { |
| 120 | + this->setFixedHeight(5*(fontMetrics().height()+4)); |
| 121 | + maxWord.append(" "); |
| 122 | + } |
| 123 | + else { |
| 124 | + this->setFixedHeight(matchNumber*(fontMetrics().height()+4)); |
| 125 | + maxWord.append(" "); |
| 126 | + } |
| 127 | + |
| 128 | + this->setFixedWidth(fontMetrics().width(maxWord)); |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void AutoComple::fontSettings(QFont f) |
| 134 | +{ |
| 135 | + setFont(f); |
| 136 | +} |
| 137 | + |
| 138 | +void AutoComple::keyPressEvent(QKeyEvent *event) |
| 139 | +{ |
| 140 | + //QPlainTextEdit::keyPressEvent(event); |
| 141 | + if ( event->key() == Qt::Key_Escape ) { |
| 142 | + close(); |
| 143 | + return; |
| 144 | + } |
| 145 | + else if (event->key() == Qt::Key_Down || |
| 146 | + event->key() == Qt::Key_Up ) { |
| 147 | + QListWidget::keyPressEvent(event); |
| 148 | + return; |
| 149 | + } |
| 150 | + else if (event->key() == Qt::Key_Left || |
| 151 | + event->key() == Qt::Key_Right ) { |
| 152 | + codeEditor->keyPressEvent(event); |
| 153 | + doAutoComple(0); |
| 154 | + return; |
| 155 | + } |
| 156 | + else if (event->key() == Qt::Key_Return) { |
| 157 | + emit itemActivated(currentItem()); |
| 158 | + close(); |
| 159 | + return; |
| 160 | + } |
| 161 | + else if ( event->modifiers() == Qt::AltModifier && event->key() == Qt::Key_N ) { |
| 162 | + QKeyEvent my(QEvent::KeyPress, Qt::Key_Down, Qt::NoModifier); |
| 163 | + keyPressEvent(&my); |
| 164 | + return; |
| 165 | + } |
| 166 | + else if ( event->modifiers() == Qt::AltModifier && event->key() == Qt::Key_P ) { |
| 167 | + QKeyEvent my(QEvent::KeyPress, Qt::Key_Up, Qt::NoModifier); |
| 168 | + keyPressEvent(&my); |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + codeEditor->keyPressEvent(event); |
| 173 | + doAutoComple(0); |
| 174 | + |
| 175 | + QListWidget::keyPressEvent(event); |
| 176 | +} |
0 commit comments