Seite 1 von 4

Texteditor QT( c++ )

Verfasst: Mo 2. Apr 2012, 17:51
von Cr0w
Ich hab mir mal auf die schnelle einen Texteditor in C++ gemacht
Kann folgendes :
I/O mit FileDialog
HTML in einer WebView anzeigen hab ich von nexi :D
Text Zoom
Auto Save

Bin noch nicht ganz fertig .... eine Syntax Highlight ist auch geplant ....

main.cpp
Spoiler :
Code:
#include "mainwindowimpl.h"
// ------------------
// V0.1 2012 QT 4.6.2
// ------------------
unsigned char nSettings_0 = 0x00;
/*
 * 1 -> Text changed
 *
 * 2 -> Web View
 *
 * // Save the Auto save intervall for the Auto save State Machine
 * 3 -> State Machine -> Auto save Bit 1
 * 4 -> State Machine -> Auto save Bit 2
 * 5 -> State Machine -> Auto save Bit 3
 *
 * 6 -> /
 * 7 -> /
 * 8 -> /
 */
unsigned char nAutoSave = 0;
QString StrSave = "";

// start
MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) : QMainWindow(parent, f)
{setupUi(this);SHOW_MainEdit MainEdit->setTextFormat(Qt::PlainText);MainEdit->setText(TEXT_EXAMPLE);}
//closeevent
void MainWindowImpl::closeEvent(QCloseEvent *event){
   if(nSettings_0&1){
   QMessageBox msgBox;
   msgBox.setText("Save ?");
   msgBox.setIcon(QMessageBox::Information);
   msgBox.setInformativeText("Do you want to save the file ?");
   msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
   msgBox.setDefaultButton(QMessageBox::Ok);
   int ret = msgBox.exec();
    switch (ret) {
         case QMessageBox::Save:SAVE_Main_Edit if(nSettings_0&1){event->ignore();}break;
         case QMessageBox::Discard:event->accept();break;
         case QMessageBox::Cancel:event->ignore();break;
         default:event->ignore();break;
       }   
   }else{event->accept();}
}
//open
void MainWindowImpl::on_action_ffnen_activated(){
QString filename = QFileDialog::getOpenFileName(
        this, tr("Open Document"),
        QDir::currentPath(),tr("Document files All files (*)") );
    if(!filename.isNull()){
       StrSave = filename.toAscii();
       QFile file(filename.toAscii());
      file.open(QIODevice::ReadOnly | QIODevice::Text);
      MainEdit->setText(file.readAll());
      file.close();
     }
}
//save
void MainWindowImpl::on_actionSpeichern_activated(){
bool B_Save = 0;unsigned char nAbort=0;
do{
     if(StrSave == ""){
       QString filename = QFileDialog::getSaveFileName( this, tr("Save Document"),QDir::currentPath(), tr("Document files All files (*)") );
       if(filename.toAscii() != "")StrSave = filename.toAscii();else nAbort=10;
       }else{B_Save = 1;}
       if(B_Save == 0){
         QMessageBox msgBox;
         msgBox.setText("Save failed !");
         msgBox.setIcon(QMessageBox::Information);
         msgBox.setInformativeText("Do you want to retry save the file ?");
         msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
         msgBox.setDefaultButton(QMessageBox::Ok);
         int ret = msgBox.exec();
          if(ret==QMessageBox::Cancel) {nSettings_0 &= 254;/*11111110*/nAbort=10;}    
      }
      nAbort++;B_Save = 1;
  }while(!Save(StrSave,MainEdit->text())||nAbort<=9);
}
// save as227
void MainWindowImpl::on_actionSpeichern_unter_activated()
{StrSave == "";SAVE_Main_Edit }
//text changed
void MainWindowImpl::on_MainEdit_textChanged(){
   if(MainEdit->text() != "")nSettings_0 |= 1;
   nAutoSave++;
      if(AutoSave((nSettings_0&28),nAutoSave))
         {SAVE_Main_Edit nAutoSave=0;}

}
// toggle HTML view
void MainWindowImpl::on_actionHML_Anzeigen_activated(int ){
   if(nSettings_0&2){nSettings_0&=253;/*11111101*/SHOW_MainEdit
   }else{nSettings_0|=2;SHOW_MainView
   MainView->setHtml(QLatin1String(MainEdit->text()));
   QTest::qSleep(20);WEB_RELOAD
   MainWindowImpl::on_actionStop_activated();
   }
}
//web control
void MainWindowImpl::on_actionReload_activated(){WEB_RELOAD}
void MainWindowImpl::on_actionStop_activated(){WEB_STOP}
void MainWindowImpl::on_actionGoogle_activated(){WEB_GO_HOME}
//zoom
void MainWindowImpl::on_actionZoom_in_activated(int ){MainEdit->zoomIn(1);}
void MainWindowImpl::on_actionZoom_out_activated(int ){MainEdit->zoomOut(1);}
// Auto Save selection
void MainWindowImpl::on_actionAll_5_changes_2_activated(){RESET_AutoSave nSettings_0 |= 4;}
void MainWindowImpl::on_actionAll_5_changes_activated(){RESET_AutoSave nSettings_0 |= 8;}
void MainWindowImpl::on_actionAll_10_changes_activated(){RESET_AutoSave nSettings_0 |= 16;}
void MainWindowImpl::on_actionAll_20_changes_activated(){RESET_AutoSave nSettings_0 |= 20;}
void MainWindowImpl::on_actionAll_40_changes_activated(){RESET_AutoSave nSettings_0 |= 24;}
void MainWindowImpl::on_actionAll_50_changes_activated(){RESET_AutoSave nSettings_0 |= 28;}
void MainWindowImpl::on_actionAuto_Backup_off_activated(){RESET_AutoSave}


main.h
Spoiler :
Code:
#include <QMainWindow>
#include <QApplication>
#include <QFileDialog>
#include <QTextBrowser>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <QMessageBox>
#include <QCloseEvent>
#include <QWebView>
#include <QTest>
#include "ui_mainwindow.h"
#ifndef MAINWINDOWIMPL_H
#define MAINWINDOWIMPL_H
#define TEXT_EXAMPLE ((1)?(!("")):("<html>\n<head>\n<title>Beschreibung der Seite</title>\n</head>\n<body>\n<h1>Hello World !</h1>\n</body>\n</html>"))
#define WEB_HOME_LINK      "https://www.google.com"
#define WEB_RELOAD      if(nSettings_0&0x02){MainView->reload();}
#define WEB_STOP      if(nSettings_0&0x02){MainView->stop();}
#define WEB_GO_HOME      if(nSettings_0&0x02){MainView->load(QUrl(WEB_HOME_LINK));}
#define SHOW_MainView    MainEdit->setVisible(0);MainView->setVisible(1);
#define SHOW_MainEdit    MainEdit->setVisible(1);MainView->setVisible(0);
#define RESET_AutoSave   nSettings_0&=227;nAutoSave=0;/*11100011*/
#define SAVE_Main_Edit  MainWindowImpl::on_actionSpeichern_activated();
//
class MainWindowImpl : public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
   MainWindowImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
private slots:
   void on_actionAuto_Backup_off_activated();
   void on_actionAll_5_changes_2_activated();
   void on_actionAll_40_changes_activated();
   void on_actionAll_5_changes_activated();
   void on_actionAll_10_changes_activated();
   void on_actionAll_20_changes_activated();
   void on_actionAll_50_changes_activated();
   void on_actionZoom_in_activated(int );
   void on_actionZoom_out_activated(int );
   void on_actionReload_activated();
   void on_actionStop_activated();
   void on_actionGoogle_activated();
   void on_actionHML_Anzeigen_activated(int );
   void on_actionSpeichern_unter_activated();
   void on_actionSpeichern_activated();
   void on_action_ffnen_activated();
   void on_MainEdit_textChanged();
   void closeEvent(QCloseEvent *event);

//save funktion
bool Save(QString StrFilename,QString StrToSave)
{
    QFile file(StrFilename.toAscii());
    if( file.exists() ){
       file.open(QIODevice::WriteOnly | QIODevice::Text);
       QTextStream out(&file);
       out << StrToSave;
       file.close();
       return(1);
         }
   return(0);
}
bool AutoSave(unsigned char nSettings_0,unsigned char nAutoSave)
{
          if(nSettings_0== 4 && nAutoSave>=2)//00000100
      {return(1);}
      else if(nSettings_0== 8 && nAutoSave>=4)//00001000
      {return(1);}
      else if(nSettings_0==12 && nAutoSave>=8)//00001100
      {return(1);}
      else if(nSettings_0==16 && nAutoSave>=10)//00010000
      {return(1);}
      else if(nSettings_0==20 && nAutoSave>=20)//00010100
      {return(1);}
      else if(nSettings_0==24 && nAutoSave>=30)//00011000
      {return(1);}
      else if(nSettings_0==28 && nAutoSave>=40)//00011100
      {return(1);}
      return(0);
}


};
#endif





Projekt Download :

Verfasst: Mo 2. Apr 2012, 17:51
von Anzeige

Re: Texteditor QT( c++ )

Verfasst: Mo 2. Apr 2012, 23:46
von Miss Rabbit
Cr0w +1

Re: Texteditor QT( c++ )

Verfasst: Di 3. Apr 2012, 20:39
von Miss Rabbit
Probier es mal mit :

Code:
QTextEdit *editor = new QTextEdit;
srchiliteqt::Qt4SyntaxHighlighter *highlighter =
 new srchiliteqt::Qt4SyntaxHighlighter(editor->document());
highlighter->init("java.lang");

Re: Texteditor QT( c++ )

Verfasst: Di 3. Apr 2012, 20:51
von Cr0w
Ja aber die gehen nicht !

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 07:08
von Karv
<textarea name = 'Texteditor' cols = '500' rows = '250'>

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 08:48
von Cr0w
Karv hat geschrieben:<textarea name = 'Texteditor' cols = '500' rows = '250'>


WAS ?
Ich glaube du verwechselst da was , das eine ist C++ das andere ist HTML ,
zur Lösung der Problem brauche ich eine Syntax Highlight Funktion ....
oder willst du wissen ob der Befehl geht ? ... Ja tut er !

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 10:10
von Miss Rabbit
Karv hat geschrieben:<textarea name = 'Texteditor' cols = '500' rows = '250'>


Vielleicht meint er dass man es auch in HTML lösen kann oder dass man die Funktion einer Seite nutzen kann ?!

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 10:12
von Miss Rabbit
Miss Rabbit hat geschrieben:Vielleicht meint er dass man es auch in HTML lösen kann oder dass man die Funktion einer Seite nutzen kann ?!


aber ich mach doch keinen Editor für HTML in HTML ! und offline soll es auch funktionieren !

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 10:33
von Karv
Das ist ein Textfeld... da kann man was reinschreiben... ergo n "editor" oder n "vi" - speichern is natürlich ne andere Sache - das war übrigens trolling... -.-

Re: Texteditor QT( c++ )

Verfasst: Mi 4. Apr 2012, 10:42
von Cr0w
Karv hat geschrieben:Das ist ein Textfeld... da kann man was reinschreiben... ergo n "editor" oder n "vi" - speichern is natürlich ne andere Sache - das war übrigens trolling... -.-


Danke für deinen "geistreichen" Beitrag , er war ganz deinem Wissensstand entsprechend - das war übrigens Ironie mit trolling !

übriges gibt es sowas schon :