/** * Provides the data to display/edit for a given index and role. * @param index The index for which data is requested * @param role The requested role * @return The relevant data */ QVariant LexerStyleModel::data(const QModelIndex& index, int role) const { const Node* node = nodeFromIndex(index); if (node == NULL || node->data() == NULL) return 0; if (node->data()->type() == StyleNode) { // Get the lexer and style ID for this node. StyleData* data = static_cast<StyleData*>(node->data()); QsciLexer* lexer = data->lexer_; int style = data->style_; switch (index.column()) { case 0: // Show language name or style name in the first column. if (role == Qt::DisplayRole) { if (style == lexer->defaultStyle()) return lexer->language(); return lexer->description(style); } break; case 1: // Show a formatted text string in the second column, using the // style's properties. return styleData(node, role); } } else { // Get the lexer and style ID for this node. PropertyData* data = static_cast<PropertyData*>(node->data()); switch (index.column()) { case 0: if (role == Qt::DisplayRole) return propertyName(data->prop_); break; case 1: return propertyData(data, role); } } return QVariant(); }
DocumentEditor::DocumentEditor(DocumentEditor* document_, QWidget *parent_) : ScintillaExt(parent_), _watcher(document_->_watcher) { _notified = true; //codec setUtf8(true); _codec = document_->_codec; _bomMode = document_->_bomMode; _hasBom = document_->_hasBom; _charsetAutoDetect = document_->_charsetAutoDetect; //document info _autoDetectEol = document_->_autoDetectEol; _autoDetectIndent = document_->_autoDetectIndent; _isNew = document_->isNew(); _fullPath = document_->getFullPath(); setDocument(document_->document()); _clone = document_; _isCloned = true; document_->_clone = this; document_->_isCloned = true; QsciLexer* l = document_->lexer(); if(l != 0) { QString lexLang = l->language(); QsciLexer* newLex = LexerManager::getInstance().lexerFactory(lexLang, this); if(newLex == 0) { //could not find the lexer newLex = LexerManager::getInstance().getAutoLexer(this); } setLexer(newLex); } //macro _macro = new QsciMacro(this); //load settings Settings settings; settings.applyToDocument(this); markerDefine(QPixmap(":/images/ledblue.png").scaled(40,16, Qt::KeepAspectRatio, Qt::SmoothTransformation), MARKER_BOOK); //connection connect(this, SIGNAL(marginClicked(int,int, Qt::KeyboardModifiers)), this, SLOT(toggleBookmark(int,int, Qt::KeyboardModifiers))); }
void DocumentEditor::toggleComment(bool lineCommentPrefered_) { QsciLexer* l = lexer(); if(l == 0) return; QString comment = l->commentLine(); QStringList commentBlock = l->commentBlock(); if(comment.isEmpty() && commentBlock.isEmpty()){ qDebug() << "Toggle comment is not supported for " << l->language(); return; } if (!hasSelectedText()) { //if line is empty, skip it int line = getCurrentLine(); if (isLineEmpty(line)) return; QString selText = text(line); selText.remove("\n"); selText.remove("\r"); QString selTextTrimmed = selText.trimmed(); int pos_start = SendScintilla(SCI_POSITIONFROMLINE, line); int pos_end = SendScintilla(SCI_GETLINEENDPOSITION, line); // check for block comments on a line if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockEnd.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); endUndoAction(); return; } } // check for single comments if (!comment.isEmpty()) { if (selTextTrimmed.startsWith(comment)) { // remove comment int idx = selText.indexOf(comment); selText = selText.remove(idx, comment.size()); } else { // set comment selText = selText.prepend(comment); } SendScintilla(SCI_SETTARGETSTART, pos_start); SendScintilla(SCI_SETTARGETEND, pos_end); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); return; } }else{ // comment out the selection QString selText = selectedText(); QString selTextTrimmed = selText.trimmed(); if (selTextTrimmed.isEmpty()) return; int lineFrom, lineTo, indexFrom, indexTo; getSelection(&lineFrom, &indexFrom, &lineTo, &indexTo); int pos_start = positionFromLineIndex(lineFrom, indexFrom); int pos_end = positionFromLineIndex(lineTo, indexTo); // check if it is double commented block - to do before single check! if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); // comment exists? remove? if (selTextTrimmed.startsWith(blockStart) && selTextTrimmed.endsWith(blockEnd)) { beginUndoAction(); int idx1 = selText.indexOf(blockStart); selText.remove(idx1, blockStart.size()); int idx2 = selText.lastIndexOf(blockEnd); selText.remove(idx2, blockEnd.size()); SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, selText.toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // check if this block can be single commented if (!comment.isEmpty() && lineCommentPrefered_) { bool empty_start = false, empty_end = false; if (indexFrom == 0) empty_start = true; else empty_start = getTextRange(positionFromLineIndex(lineFrom, 0), pos_start).trimmed().isEmpty(); if (indexTo == 0) empty_end = true; else empty_end = getTextRange(pos_end, positionFromLineIndex(lineTo+1, 0)).trimmed().isEmpty(); if (empty_start && empty_end) { beginUndoAction(); // corrections if (indexTo == 0) lineTo--; if (isLineEmpty(lineFrom)) { lineFrom++; indexFrom = 0; } // a workaround: move cursor to the next line to replace EOL as well setSelection(lineFrom, 0, lineTo+1, 0); QStringList sl; for (int i = lineFrom; i <= lineTo; i++) sl += text(i); bool comm = false; for (int i = 0; i < sl.count(); i++) if (!sl.at(i).trimmed().startsWith(comment)) { comm = true; break; } for (int i = 0; i < sl.count(); i++) { if (comm) sl[i] = sl[i].prepend(comment); else { int idx = sl.at(i).indexOf(comment); sl[i] = sl[i].remove(idx, comment.size()); } } SendScintilla(SCI_TARGETFROMSELECTION); SendScintilla(SCI_REPLACETARGET, -1, sl.join("").toUtf8().data()); SendScintilla(SCI_SETSEL, SendScintilla(SCI_GETTARGETSTART), SendScintilla(SCI_GETTARGETEND)); endUndoAction(); return; } } // else, set double comment if(commentBlock.size() >= 2){ QString blockStart = commentBlock.first(); QString blockEnd = commentBlock.last(); beginUndoAction(); // last is first SendScintilla(SCI_INSERTTEXT, pos_end, blockEnd.toUtf8().data()); SendScintilla(SCI_INSERTTEXT, pos_start, blockStart.toUtf8().data()); // select everything if(lineFrom == lineTo) setSelection(lineFrom, indexFrom, lineTo, indexTo + blockStart.size() + blockEnd.size()); else setSelection(lineFrom, indexFrom, lineTo, indexTo + blockEnd.size()); endUndoAction(); return; } } }
QString DocumentEditor::getType() const { QsciLexer* lex = lexer(); if(lex) return lex->language(); return "Normal Text"; }