📄 keditcl1.cpp
字号:
// Second Pass let's see whether we can fill each line more /* printf("PASS 2\n"); for ( int i = 0 ; i < (int)par.count() ; i ++){ printf("%s\n",par.at(i)); } printf("\n"); */ for (i = 0 ; i < (int)par.count() - 1 ; i ++){ // printf("par.count %d line %d\n",par.count(),i); int k = 0; int l = 0; int last_ok = 0; int free = 0; pstring = par.at(i); // printf("LENGTH of line %d = %d\n",i,pstring.length()); for( k = 0, l = 0; k < (int) pstring.length() && l <= fill_column_value; k++){ if(pstring.data()[k] == '\t') l +=8 - l%8; else l ++; if( l <= fill_column_value ) last_ok = k; } if( l >= fill_column_value) // line is full go on to next line continue; free = fill_column_value - l -1; // -1 for the extra space we need to insert // printf("free %d\n",free); mstring = par.at(i+1); for( k = 0, l = 0; k < (int) mstring.length() && l <= fill_column_value; k++){ if(mstring.data()[k] == '\t') l +=8 - l%8; else l ++; if( l <= free ) last_ok = k; } if( (int) l <= free){ pstring = pstring + QString(" ") + mstring; par.remove(i); par.insert(i,pstring); par.remove(i+1); i --; // check again whether line i is full now } else{ space_pos = mstring.findRev( " ", QMIN(free,last_ok), TRUE ); if(space_pos == -1) // well let's try to find a TAB then .. space_pos = mstring.findRev( "\t", QMIN(free,last_ok), TRUE ); if(space_pos == -1) continue; // can't find a word to add to the previous line pstring = pstring + QString(" ") + mstring.left(space_pos ); //printf("adding: %s %d\n",mstring.left(space_pos).data(),space_pos); par.remove(i); par.insert(i,pstring); mstring = mstring.mid(space_pos + 1,mstring.length()); par.remove(i+1); par.insert(i+1,mstring); } } /* printf("PASS 3\n"); for ( int i = 0 ; i < (int)par.count() ; i ++){ printf("%s\n",par.at(i)); } printf("\n"); */ QString prerun; for (i = 0 ; i < (int)par.count() ; i ++){ prerun = prefix + (QString) par.at(i); par.remove(i); par.insert(i,prerun); } /* printf("PASS 4\n"); for ( int i = 0 ; i < (int)par.count() ; i ++){ printf("%s\n",par.at(i)); } printf("\n"); */ fill_column_value = fill_column; return true;}void KEdit::getpar2(int line,QStrList& par,int& upperbound,QString& prefix){ int templine,tempcol; //Matthias getCursorPosition(&templine,&tempcol);//Matthias QString linestr; QString string, string2; bool foundone = false; int line2; line2 = line; string = textLine(line2); string = string.stripWhiteSpace(); if(string.isEmpty()){ upperbound = 0; par.clear(); return; } while(line2 >= 0){ string = textLine(line2); string2 = string.stripWhiteSpace(); if(string2.isEmpty()){ foundone = true; break; } line2 --; } if(foundone) line2 ++; if(line2 < 0) line2 = 0; upperbound = line2; par.clear(); prefix = prefixString(textLine(line2)); int num = numLines(); for(int i = line2 ; i < num ; i++){ linestr = textLine(line2); if (i == templine){ // Matthias // cursor is in this line. At least it was. Insert mark. linestr.insert(tempcol, (char)1); } if((linestr.stripWhiteSpace()).isEmpty()) break; if(reduce_white_on_justify){ linestr = linestr.stripWhiteSpace(); } par.append(linestr); removeLine(line2); }}void KEdit::mynewLine(){ if (isReadOnly()) return; setModified(); if(!autoIndentMode){ // if not indent mode newLine(); return; } int line,col, line2; bool found_one = false; getCursorPosition(&line,&col); line2 = line; QString string, string2; while(line >= 0){ string = textLine(line); string2 = string.stripWhiteSpace(); if(!string2.isEmpty()){ string = prefixString(string); found_one = TRUE; break; } line --; } // string will now contain those whitespace characters that I need to insert // on the next line. if(found_one){ newLine(); insertAt(string.data(),line2 + 1,0); } else{ newLine(); }}void KEdit::setAutoIndentMode(bool mode){ autoIndentMode = mode;}QString KEdit::prefixString(QString string){ // This routine return the whitespace before the first non white space // character in string. This is used in mynewLine() for indent mode. // It is assumed that string contains at least one non whitespace character // ie \n \r \t \v \f and space // printf(":%s\n",string.data()); int size = string.size(); char* buffer = (char*) malloc(size + 1); strncpy (buffer, string.data(),size - 1); buffer[size] = '\0'; int i; for (i = 0 ; i < size; i++){ if(!isspace(buffer[i])) break; } buffer[i] = '\0'; QString returnstring = buffer; free(buffer); // printf(":%s:\n",returnstring.data()); return returnstring;}void KEdit::mousePressEvent (QMouseEvent* e){ QMultiLineEdit::mousePressEvent(e); emit CursorPositionChanged();}void KEdit::mouseMoveEvent (QMouseEvent* e){ QMultiLineEdit::mouseMoveEvent(e); emit CursorPositionChanged();}void KEdit::installRBPopup(QPopupMenu* p){ rb_popup = p;}void KEdit::mouseReleaseEvent (QMouseEvent* e){ QMultiLineEdit::mouseReleaseEvent(e); emit CursorPositionChanged();}int KEdit::saveFile(){ struct stat st; int stat_ok = -1; bool exists_already; if(!modified) { emit saving(); mykapp->processEvents(); return KEDIT_OK; } QFile file(filename); QString backup_filename; exists_already = file.exists(); if(exists_already){ stat_ok = stat(filename.data(), &st); backup_filename = filename; backup_filename.detach(); backup_filename += '~'; rename(filename.data(),backup_filename.data()); } if( !file.open( IO_WriteOnly | IO_Truncate )) { rename(backup_filename.data(),filename.data()); QMessageBox::warning( this, klocale->translate("Sorry"), klocale->translate("Could not save the document\n"), klocale->translate("OK"), "", "", 0,0 ); return KEDIT_OS_ERROR; } emit saving(); mykapp->processEvents(); QTextStream t(&file); int line_count = numLines(); for(int i = 0 ; i < line_count ; i++){ t << textLine(i) << '\n'; } modified = FALSE; file.close(); if(exists_already) chmod(filename.data(),st.st_mode);// preseve filepermissions return KEDIT_OK;}void KEdit::setFileName(char* name){ filename = name; filename.detach();}void KEdit::saveasfile(char* name){ QString filenamebackup; filenamebackup = filename; filename = name; filename.detach(); saveFile(); filename = filenamebackup; filename.detach();}QFileDialog* KEdit::getFileDialog(const char* captiontext){ if(!file_dialog){ file_dialog = new QFileDialog(current_directory.data(),"*",this,"file_dialog",TRUE); } file_dialog->setCaption(captiontext); file_dialog->rereadDir(); return file_dialog;}int KEdit::saveAs(){ QFileDialog *box; QFileInfo info; QString tmpfilename; int result; box = getFileDialog(klocale->translate("Save Document As")); QPoint point = this->mapToGlobal (QPoint (0,0)); QRect pos = this->geometry(); box->setGeometry(point.x() + pos.width()/2 - box->width()/2, point.y() + pos.height()/2 - box->height()/2, box->width(),box->height());try_again: box->show(); if (!box->result()) { return KEDIT_USER_CANCEL; } if(box->selectedFile().isEmpty()){ return KEDIT_USER_CANCEL; } info.setFile(box->selectedFile()); if(info.exists()){ switch( QMessageBox::warning( this, klocale->translate("Warning:"), klocale->translate("A Document with this Name exists already\n"\ "Do you want to overwrite it ?"), klocale->translate("Yes"), klocale->translate("No"), "", 1, 1 ) ){ case 0: // Yes or Enter // try again break; case 1: // No or Escape goto try_again; break; } } tmpfilename = filename; filename = box->selectedFile(); // we need this for saveFile(); modified = TRUE; result = saveFile(); if( result != KEDIT_OK) filename = tmpfilename; // revert filename return result;}int KEdit::doSave(){ int result = 0; if(filename == klocale->translate("Untitled")) { result = saveAs(); if(result == KEDIT_OK) setCaption(filename); return result; } QFileInfo info(filename); if(info.exists() && !info.isWritable()){ QMessageBox::warning( this, klocale->translate("Sorry:"), klocale->translate("You do not have write permission "\ "to this file.\n"), klocale->translate("OK"), "", "", 0,0 ); return KEDIT_NOPERMISSIONS; } result = saveFile(); return result;}int KEdit::doSave( const char *_name ){ QString temp = filename; filename = _name; filename.detach(); int result = saveFile(); filename = temp; filename.detach(); return result;}void KEdit::setName( const char *_name ){ filename = _name; filename.detach();}QString KEdit::getName(){ return filename;}void KEdit::saveBackupCopy(bool par){ make_backup_copies = par;}void KEdit::selectFont(){ QFont font = this->font(); KFontDialog::getFont(font); this->setFont(font);}void KEdit::setModified(){ modified = TRUE;}void KEdit::toggleModified( bool _mod ){ modified = _mod;}bool KEdit::isModified(){ return modified;}void KEdit::setContextSens(){}bool KEdit::eventFilter(QObject *o, QEvent *ev){ static QPoint tmp_point; (void) o; if (ev->type() == Event_Paint) { if (srchdialog) if (srchdialog->isVisible()) srchdialog->raise(); if (replace_dialog) if (replace_dialog->isVisible()) replace_dialog->raise(); } if(ev->type() != Event_MouseButtonPress) return FALSE; QMouseEvent *e = (QMouseEvent *)ev; if(e->button() != RightButton) return FALSE; tmp_point = QCursor::pos(); if(rb_popup) rb_popup->popup(tmp_point); return TRUE;}QString KEdit::markedText(){ return QMultiLineEdit::markedText();}void KEdit::doGotoLine() { if( !gotodialog ) gotodialog = new KEdGotoLine( this, "gotodialog" ); this->clearFocus(); gotodialog->show(); // this seems to be not necessary // gotodialog->setFocus(); if( gotodialog->result() ) { setCursorPosition( gotodialog->getLineNumber()-1 , 0, FALSE ); emit CursorPositionChanged(); setFocus(); }}void KEdit::setReduceWhiteOnJustify(bool reduce){ reduce_white_on_justify = reduce;}#include "keditcl.moc"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -