Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #27

Merged
merged 2 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/code_editor.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/line_numbers.cpython-311.pyc
Binary file not shown.
Binary file modified __pycache__/menu_bar.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
74 changes: 74 additions & 0 deletions components/code_editor/code_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from PyQt5.QtWidgets import QPlainTextEdit, QFrame, QVBoxLayout, QWidget
from PyQt5.QtCore import Qt
from .syntax_highlighter import SyntaxHighlighter
from .line_numbers import LineNumberArea
from PyQt5.QtGui import QPainter

class CodeEditor(QFrame):
def __init__(self, parent=None):
super().__init__(parent)
self.initUI()

def initUI(self):
# Create a layout for the code editor and line numbers
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)

# Create the code editor widget
self.code_editor = QPlainTextEdit(self)
self.code_editor.setLineWrapMode(QPlainTextEdit.NoWrap)

# Create a syntax highlighter for the code editor
self.highlighter = SyntaxHighlighter(self.code_editor.document())

# Add code editor to the layout
layout.addWidget(self.code_editor)

# Create the line numbers widget
self.line_numbers = LineNumberArea(self.code_editor)

# Set the code editor's viewport margin to the width of the line numbers
self.code_editor.setViewportMargins(self.line_numbers.width(), 0, 0, 0)

# Connect the code editor's scrollbar to update the line numbers
self.code_editor.verticalScrollBar().valueChanged.connect(self.update_line_numbers)

# Initialize line numbers
self.init_line_numbers()

self.setLayout(layout)

def init_line_numbers(self):
self.code_editor.verticalScrollBar().valueChanged.connect(self.line_numbers.update)

def line_number_width(self):
digits = 1
max_value = max(1, self.code_editor.blockCount())
while max_value >= 10:
max_value /= 10
digits += 1
space = 3 + self.code_editor.fontMetrics().width('9') * digits
return space

def line_number_paint(self, event):
painter = QPainter(self.line_numbers)
painter.fillRect(event.rect(), Qt.lightGray)
block = self.code_editor.firstVisibleBlock()
block_number = block.blockNumber()
top = self.code_editor.blockBoundingGeometry(block).translated(self.code_editor.contentOffset()).top()
bottom = top + self.code_editor.blockBoundingRect(block).height()

# Iterate over visible blocks and paint line numbers
while block.isValid() and (top <= event.rect().bottom()):
if block.isVisible() and (bottom >= event.rect().top()):
number = str(block_number + 1)
painter.setPen(Qt.black)
painter.drawText(0, top, self.line_numbers.width(), self.code_editor.fontMetrics().height(), Qt.AlignRight, number)
block = block.next()
top = bottom
bottom = top + self.code_editor.blockBoundingRect(block).height()
block_number += 1
painter.end()

def update_line_numbers(self):
self.line_numbers.update()
34 changes: 34 additions & 0 deletions components/code_editor/line_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from PyQt5.QtWidgets import QWidget
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QPainter

class LineNumberArea(QWidget):
def __init__(self, editor):
super().__init__(editor)
self.code_editor = editor

def sizeHint(self):
return QSize(self.line_number_width(), 0)

def line_number_width(self):
digits = len(str(self.code_editor.blockCount()))
space = 3 + self.fontMetrics().width('9') * digits
return space

def paintEvent(self, event):
painter = QPainter(self)
painter.fillRect(event.rect(), Qt.lightGray)
block = self.code_editor.firstVisibleBlock()
block_number = block.blockNumber() + 1
top = self.code_editor.blockBoundingGeometry(block).translated(self.code_editor.contentOffset()).top()
bottom = top + self.code_editor.blockBoundingRect(block).height()

while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
text = str(block_number)
painter.setPen(Qt.black)
painter.drawText(0, top, self.width(), self.fontMetrics().height(), Qt.AlignRight | Qt.AlignVCenter, text)
block = block.next()
top = bottom
bottom = top + self.code_editor.blockBoundingRect(block).height()
block_number += 1
20 changes: 20 additions & 0 deletions components/code_editor/syntax_highlighter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from PyQt5.QtGui import QTextCharFormat, QTextDocument
from PyQt5.QtWidgets import QPlainTextEdit
from PyQt5.QtGui import QSyntaxHighlighter, QSyntaxHighlighter
from PyQt5.QtCore import Qt, QTextStream
from traitlets import CRegExp

class SyntaxHighlighter(QSyntaxHighlighter):
def __init__(self, parent):
super().__init__(parent)
self.highlighting_rules = []

# Define highlighting rules (e.g., for keywords, comments, etc.)

def highlightBlock(self, text):
for pattern, format in self.highlighting_rules:
regex = CRegExp(pattern)
iterator = regex.globalMatch(text)
while iterator.hasNext():
match = iterator.next()
self.setFormat(match.capturedStart(), match.capturedLength(), format)
Binary file not shown.
24 changes: 24 additions & 0 deletions components/menu_bar/menu_bar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from PyQt5.QtWidgets import QMainWindow, QMenuBar, QMenu, QAction

def create_menu_bar(root):
menubar = root.menuBar()

# Create a "File" menu
file_menu = menubar.addMenu("File")

# Add menu items to the "File" menu
new_action = QAction("New", root)
open_action = QAction("Open", root)
save_action = QAction("Save", root)
save_as_action = QAction("Save As", root)
exit_action = QAction("Exit", root)

file_menu.addAction(new_action)
file_menu.addAction(open_action)
file_menu.addAction(save_action)
file_menu.addAction(save_as_action)
file_menu.addSeparator() # Add a separator line
file_menu.addAction(exit_action)

# Connect menu items to functions or actions as needed
# For example, exit_action.triggered.connect(on_exit)
38 changes: 18 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import tkinter as tk
from menu_bar import create_menu_bar
from tkinter import scrolledtext
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from components.code_editor.code_editor import CodeEditor
from components.menu_bar.menu_bar import create_menu_bar

def create_base_window():
root = tk.Tk()
root.title("My Python IDE")

# Create a menu bar using the imported function
create_menu_bar(root)
app = QApplication(sys.argv)
root = QMainWindow()
root.setWindowTitle("My Python IDE")
root.setGeometry(100, 100, 800, 600) # Set the window size

# Create the custom code editor component
code_editor = CodeEditor(root)
root.setCentralWidget(code_editor)

# Create a text widget (for code editing)
text_widget = scrolledtext.ScrolledText(
root,
wrap=tk.WORD,
font=("Consolas", 12), # Use a monospaced font like Consolas
insertbackground="Black", # Color of the cursor
selectbackground="lightblue" # Color of selected text
)
text_widget.pack(expand=tk.YES, fill=tk.BOTH)
# Create the menu bar
create_menu_bar(root)

return root
return app, root

if __name__ == "__main__":
app = create_base_window()
app.mainloop()
app, main_window = create_base_window()
main_window.show()
sys.exit(app.exec_())
33 changes: 0 additions & 33 deletions menu_bar.py

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@

PyQt5
Loading