0% found this document useful (0 votes)
4 views3 pages

Main Window Py Correction 13

The document outlines a Python application using PyQt5 for a text editor called 'NVivo Clone'. It features functionalities such as tag management, file operations (open, save, export), and a theme toggle, alongside an auto-save feature. The main window contains a text editor, a tag list with search capabilities, and a menu bar for various actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Main Window Py Correction 13

The document outlines a Python application using PyQt5 for a text editor called 'NVivo Clone'. It features functionalities such as tag management, file operations (open, save, export), and a theme toggle, alongside an auto-save feature. The main window contains a text editor, a tag list with search capabilities, and a menu bar for various actions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import os import json from PyQt5.

QtWidgets import ( QMainWindow, QFileDialog,


QMessageBox, QVBoxLayout, QWidget, QSplitter, QListWidget, QTextEdit, QAction,
QMenuBar, QLineEdit, QPushButton, QHBoxLayout, QLabel ) from PyQt5.QtCore import Qt,
QTimer from database.models import Tag from database.database import Session

class MainWindow(QMainWindow): def init(self): super().init() self.setWindowTitle("NVivo


Clone") self.setGeometry(100, 100, 1000, 600)

self.session = Session()
self.theme = 'light'

self.init_ui()
self.load_tags()
self.start_auto_save()

def init_ui(self):
self.text_edit = QTextEdit()

# Tag List and Search


self.search_bar = QLineEdit()
self.search_bar.setPlaceholderText("Search tags...")
self.search_bar.textChanged.connect(self.filter_tags)

self.tag_list = QListWidget()
self.tag_list.setMaximumWidth(200)
self.tag_list.itemClicked.connect(self.apply_tag_to_selection)

self.refresh_tags_button = QPushButton("Refresh Tags")


self.refresh_tags_button.clicked.connect(self.load_tags)

tag_layout = QVBoxLayout()
tag_layout.addWidget(QLabel("Tags"))
tag_layout.addWidget(self.search_bar)
tag_layout.addWidget(self.tag_list)
tag_layout.addWidget(self.refresh_tags_button)

tag_widget = QWidget()
tag_widget.setLayout(tag_layout)

splitter = QSplitter(Qt.Horizontal)
splitter.addWidget(tag_widget)
splitter.addWidget(self.text_edit)

container = QWidget()
layout = QVBoxLayout()
layout.addWidget(splitter)
container.setLayout(layout)
self.setCentralWidget(container)

self.create_menu()

def create_menu(self):
menubar = self.menuBar()

file_menu = menubar.addMenu("File")
open_action = QAction("Open", self)
open_action.triggered.connect(self.open_file)
file_menu.addAction(open_action)

save_action = QAction("Save", self)


save_action.triggered.connect(self.save_file)
file_menu.addAction(save_action)

export_action = QAction("Export Tags", self)


export_action.triggered.connect(self.export_tags)
file_menu.addAction(export_action)

view_menu = menubar.addMenu("View")
toggle_theme_action = QAction("Toggle Theme", self)
toggle_theme_action.triggered.connect(self.toggle_theme)
view_menu.addAction(toggle_theme_action)

help_menu = menubar.addMenu("Help")
about_action = QAction("About", self)
about_action.triggered.connect(self.show_about)
help_menu.addAction(about_action)

def open_file(self):
path, _ = QFileDialog.getOpenFileName(self, "Open Text File", "", "Text
Files (*.txt)")
if path:
with open(path, 'r', encoding='utf-8') as file:
self.text_edit.setPlainText(file.read())

def save_file(self):
path, _ = QFileDialog.getSaveFileName(self, "Save Text File", "", "Text
Files (*.txt)")
if path:
with open(path, 'w', encoding='utf-8') as file:
file.write(self.text_edit.toPlainText())

def export_tags(self):
tags = self.session.query(Tag).all()
tag_data = [{'name': tag.name, 'description': tag.description} for tag in
tags]
path, _ = QFileDialog.getSaveFileName(self, "Export Tags", "", "JSON Files
(*.json)")
if path:
with open(path, 'w', encoding='utf-8') as file:
json.dump(tag_data, file, indent=4)

def load_tags(self):
self.tag_list.clear()
tags = self.session.query(Tag).all()
self.current_tags = tags
for tag in tags:
self.tag_list.addItem(tag.name)

def filter_tags(self, text):


self.tag_list.clear()
for tag in self.current_tags:
if text.lower() in tag.name.lower():
self.tag_list.addItem(tag.name)

def apply_tag_to_selection(self, item):


cursor = self.text_edit.textCursor()
if cursor.hasSelection():
tag = item.text()
selection = cursor.selectedText()
cursor.insertText(f'[{tag}:{selection}]')

def toggle_theme(self):
if self.theme == 'light':
self.setStyleSheet("QTextEdit, QListWidget, QLineEdit { background-
color: #2b2b2b; color: #f0f0f0; } QPushButton { background-color: #444; color:
white; }")
self.theme = 'dark'
else:
self.setStyleSheet("")
self.theme = 'light'

def show_about(self):
QMessageBox.information(self, "About NVivo Clone", "NVivo Clone App\nBuilt
with Python and PyQt5\nDeveloped by Temwa")

def start_auto_save(self):
self.auto_save_timer = QTimer(self)
self.auto_save_timer.timeout.connect(self.auto_save)
self.auto_save_timer.start(60000)

def auto_save(self):
auto_path = os.path.join(os.getcwd(), 'autosave.txt')
with open(auto_path, 'w', encoding='utf-8') as file:
file.write(self.text_edit.toPlainText())

You might also like