944,171 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 633
  • Java RSS
Oct 31st, 2009
0

CodeEditor = problem

Expand Post »
Hello,

I have a problem which is this:

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjiu is offline Offline
16 posts
since Dec 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
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; Oct 31st, 2009 at 12:16 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 31st, 2009
-1
Re: CodeEditor = problem
thank's but I don't know how to write this code.. can you write my code

please!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjiu is offline Offline
16 posts
since Dec 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
use method (from documentation of package mentioned above)
Quote ...
void setDocument(QTextDocument document)
Makes document the new document of the text editor.
Last edited by quuba; Oct 31st, 2009 at 3:51 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
sorry for that, I am beginner of learning Java

where I must put :

Java Syntax (Toggle Plain Text)
  1. void setDocument(QTextDocument document)

Java Syntax (Toggle Plain Text)
  1. new Highlighter(textEdit.document());
  2. new CodeEditor(textEdit.document());
  3. }
  4. public class CodeEditor extends QPlainTextEdit
  5.  
  6. {
  7. public CodeEditor(QTextDocument parent)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjiu is offline Offline
16 posts
since Dec 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
Java Syntax (Toggle Plain Text)
  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; Oct 31st, 2009 at 4:55 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
hmmm the error is:

QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout

Java Syntax (Toggle Plain Text)
  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);
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjiu is offline Offline
16 posts
since Dec 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
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
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 31st, 2009
0
Re: CodeEditor = problem
Quote ...
run:
QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout
BUILD SUCCESSFUL (total time: 8 seconds)
Java Syntax (Toggle Plain Text)
  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. }
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 1st, 2009
0
Re: CodeEditor = problem
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; Nov 1st, 2009 at 3:32 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kjiu is offline Offline
16 posts
since Dec 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to get control on web links.
Next Thread in Java Forum Timeline: tic-tac-toe!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC