CodeEditor = problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2008
Posts: 16
Reputation: kjiu is an unknown quantity at this point 
Solved Threads: 0
kjiu kjiu is offline Offline
Newbie Poster

CodeEditor = problem

 
0
  #1
33 Days Ago
Hello,

I have a problem which is this:

  1. private void setupEditor()
  2. {
  3. QFont font = new QFont();
  4. font.setFamily("Lucida Console");
  5. font.setFixedPitch(true);
  6. font.setPointSize(10);
  7.  
  8. textEdit = new QTextEdit();
  9. textEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap);
  10. textEdit.setFont(font);
  11.  
  12. new Highlighter(textEdit.document());
  13. new CodeEditor(textEdit.document());
  14.  
  15. }
  16. public class CodeEditor extends QPlainTextEdit
  17. {
  18. public CodeEditor(QTextDocument textDocument)
  19. // public CodeEditor()
  20. {
  21. lineNumberArea = new LineNumberArea(this);
  22.  
  23. blockCountChanged.connect(this, "updateLineNumberAreaWidth(Integer)");
  24. updateRequest.connect(this, "updateLineNumberArea(QRect,Integer)");
  25. cursorPositionChanged.connect(this, "highlightCurrentLine()");
  26.  
  27. updateLineNumberAreaWidth(0);
  28. highlightCurrentLine();
  29.  
  30. }
  31. public int lineNumberAreaWidth()
  32. {
  33. int digits = 1;
  34. int max = Math.max(1, blockCount());
  35. while (max >= 10) {
  36. max /= 10;
  37. ++digits;
  38. }
  39.  
  40. int space = 3 + fontMetrics().width('9') * digits;
  41.  
  42. return space;
  43. }
  44. public void updateLineNumberAreaWidth(Integer newBlockCount)
  45. {
  46. setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
  47. }
  48. public void updateLineNumberArea(QRect rect, Integer dy)
  49. {
  50. if (dy > 0)
  51. lineNumberArea.scroll(0, dy);
  52. else
  53. lineNumberArea.update(0, rect.y(), lineNumberArea.width(),
  54. rect.height());
  55.  
  56. if (rect.contains(viewport().rect()))
  57. updateLineNumberAreaWidth(0);
  58. }
  59. protected void resizeEvent(QResizeEvent e)
  60. {
  61. super.resizeEvent(e);
  62.  
  63. QRect cr = contentsRect();
  64. lineNumberArea.setGeometry(new QRect(cr.left(), cr.top(),
  65. lineNumberAreaWidth(), cr.height()));
  66. }
  67. private void highlightCurrentLine()
  68. {
  69. List<QTextEdit_ExtraSelection> extraSelections =
  70. new Vector<QTextEdit_ExtraSelection>();
  71.  
  72. if (!isReadOnly()) {
  73. QTextEdit_ExtraSelection selection =
  74. new QTextEdit_ExtraSelection();
  75.  
  76. QColor lineColor = QColor.yellow.lighter(160);
  77.  
  78. QTextCharFormat format = selection.format();
  79. format.setBackground(new QBrush(lineColor));
  80. format.setProperty(QTextFormat.Property.FullWidthSelection.value(), new Boolean(true));
  81. selection.setFormat(format);
  82. QTextCursor cursor = textCursor();
  83. cursor.clearSelection();
  84. selection.setCursor(cursor);
  85. extraSelections.add(selection);
  86. }
  87.  
  88. setExtraSelections(extraSelections);
  89. }
  90. public void lineNumberAreaPaintEvent(QPaintEvent event)
  91. {
  92. QPainter painter = new QPainter(lineNumberArea);
  93. painter.setPen(new QPen(QColor.black));
  94. painter.fillRect(event.rect(), new QBrush(QColor.lightGray));
  95.  
  96. QTextBlock block = firstVisibleBlock();
  97. int blockNumber = block.blockNumber();
  98. int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
  99. int bottom = top + (int) blockBoundingRect(block).height();
  100.  
  101. while (block.isValid() && top <= event.rect().bottom()) {
  102. if (block.isVisible() && bottom >= event.rect().top()) {
  103. String number = String.valueOf(blockNumber + 1);
  104. painter.drawText(0, top, lineNumberArea.width(), fontMetrics().height(),
  105. Qt.AlignmentFlag.AlignRight.value(), number);
  106. }
  107.  
  108. block = block.next();
  109. top = bottom;
  110. bottom = top + (int) blockBoundingRect(block).height();
  111. ++blockNumber;
  112. }
  113. }
  114.  
  115. private class LineNumberArea extends QWidget
  116. {
  117. public LineNumberArea(CodeEditor editor)
  118. {
  119. codeEditor = editor;
  120. setParent(codeEditor);
  121. }
  122. public QSize sizeHint()
  123. {
  124. return new QSize(codeEditor.lineNumberAreaWidth(), 0);
  125. }
  126. protected void paintEvent(QPaintEvent event)
  127. {
  128. codeEditor.lineNumberAreaPaintEvent(event);
  129. }
  130. private CodeEditor codeEditor;
  131. }
  132.  
  133. private LineNumberArea lineNumberArea;
  134. }

and the problem is there:

new CodeEditor(textEdit.document());

it does't appear at TextEdit

Kjiu
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
33 Days Ago
You are working with package qtjambi. You should know it better.

first read
http://doc.trolltech.com/qtjambi-4.5.2_01/index.html
http://doc.trolltech.com/qtjambi-4.5...nTextEdit.html

select proper method from QPlainTextEdit to apply textEdit.document() in CodeEditor constructor
Last edited by quuba; 33 Days Ago at 12:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 16
Reputation: kjiu is an unknown quantity at this point 
Solved Threads: 0
kjiu kjiu is offline Offline
Newbie Poster
 
-1
  #3
32 Days Ago
thank's but I don't know how to write this code.. can you write my code

please!!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
32 Days Ago
use method (from documentation of package mentioned above)
void setDocument(QTextDocument document)
Makes document the new document of the text editor.
Last edited by quuba; 32 Days Ago at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 16
Reputation: kjiu is an unknown quantity at this point 
Solved Threads: 0
kjiu kjiu is offline Offline
Newbie Poster
 
0
  #5
32 Days Ago
sorry for that, I am beginner of learning Java

where I must put :

  1. void setDocument(QTextDocument document)

  1. new Highlighter(textEdit.document());
  2. new CodeEditor(textEdit.document());
  3. }
  4. public class CodeEditor extends QPlainTextEdit
  5.  
  6. {
  7. public CodeEditor(QTextDocument parent)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #6
32 Days Ago
  1. public class CodeEditor extends QPlainTextEdit
  2. {
  3. public CodeEditor(QTextDocument textDocument) //textDocument as parameter in constructor
  4. // public CodeEditor()
  5. {
  6. // here
  7. setDocument(textDocument); // the same textDocument as argument in method setDocument(
  8.  
  9. lineNumberArea = new LineNumberArea(this);
  10. // .......
Learn Passing Information to a Method or a Constructor
http://java.sun.com/docs/books/tutor...arguments.html
Last edited by quuba; 32 Days Ago at 4:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 16
Reputation: kjiu is an unknown quantity at this point 
Solved Threads: 0
kjiu kjiu is offline Offline
Newbie Poster
 
0
  #7
32 Days Ago
hmmm the error is:

QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout

  1. private void setupEditor()
  2. {
  3. QFont font = new QFont();
  4. font.setFamily("Arial");
  5. font.setFixedPitch(true);
  6. font.setPointSize(10);
  7.  
  8. textEdit = new QTextEdit();
  9. textEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap);
  10. textEdit.setFont(font);
  11.  
  12. new Highlighter(textEdit.document());
  13. new CodeEditor(textEdit.document());
  14. }
  15. public class CodeEditor extends QPlainTextEdit
  16.  
  17. {
  18. public CodeEditor(QTextDocument textDocument)
  19. // public CodeEditor()
  20. {
  21. setDocument(textDocument);
  22. lineNumberArea = new LineNumberArea(this);
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #8
32 Days Ago
Summary of conversation.
I know, You want to use the package trolltech to create applications.
As a beginner java programmer you cetainly think "this is the shortest way to achieve rapid results".
As you can see, the use of the package trolltech requires knowledge of two things: first - knowledge of java, a second -knowledge of the specific package trolltech. I think that I have some basic knowledge on java, but I do not know trolltech package.
I can help with the java language problems, but not with a specific package.
For this, I recommend systematic learning the Java language and the Java API, that allows you to create programs with a greater perspective. You can do this, as I did and still do, being self-taught.
See you later on Dani Web pages.

quuba
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #9
32 Days Ago
run:
QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout
BUILD SUCCESSFUL (total time: 8 seconds)
  1. public static void main(String args[]) {
  2. QApplication.initialize(args);
  3. //
  4. QFont font = new QFont();
  5. font.setFamily("Lucida Console");
  6. font.setFixedPitch(true);
  7. font.setPointSize(30);
  8. QTextEdit textEdit = new QTextEdit();
  9. // textEdit.setLineWrapMode(QTextEdit.LineWrapMode.NoWrap);
  10. textEdit.setFont(font);
  11. textEdit.insertHtml("<H1><HR WIDTH=\"100%\">Applet HTML Page<HR WIDTH=\"100%\"></H1><P>aaaaaaaaaaaa</P>");
  12.  
  13. new CodeEditor(textEdit).show();
  14. QApplication.exec();
  15. }
  1. public class CodeEditor extends QTextEdit {
  2.  
  3. //![constructor]
  4. public CodeEditor(QTextEdit textEdit) {
  5. super.setDocument(textEdit.document());
  6.  
  7. //lineNumberArea = new LineNumberArea(this);
  8.  
  9. //blockCountChanged.connect(this, "updateLineNumberAreaWidth(Integer)");
  10. //updateRequest.connect(this, "updateLineNumberArea(QRect,Integer)");
  11. cursorPositionChanged.connect(this, "highlightCurrentLine()");
  12.  
  13. //updateLineNumberAreaWidth(0);
  14. highlightCurrentLine();
  15.  
  16. setWindowTitle("Code Editor Example");
  17. setWindowIcon(new QIcon("classpath:com/trolltech/images/qt-logo.png"));
  18. }
textEdit is type of QTextEdit then
CodeEditor must extend QTextEdit
commented lines does not work in case QTextEdit.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 16
Reputation: kjiu is an unknown quantity at this point 
Solved Threads: 0
kjiu kjiu is offline Offline
Newbie Poster
 
0
  #10
32 Days Ago
thanks, but I wanted to have Highlighter and CodeEditor.

I want to be like this :

http://i37.tinypic.com/esixyh.png
Last edited by kjiu; 32 Days Ago at 3:32 am.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC