0% found this document useful (0 votes)
6 views6 pages

Main Window Py 14

The document outlines the implementation of a PyQt5 application called 'NVivo Clone', which features a main window with text editing capabilities and a tag management system. Users can add, filter, and apply tags to selected text, as well as open, save, and export files. The application also includes a dark mode toggle and an auto-save functionality that saves the text content every minute.
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)
6 views6 pages

Main Window Py 14

The document outlines the implementation of a PyQt5 application called 'NVivo Clone', which features a main window with text editing capabilities and a tag management system. Users can add, filter, and apply tags to selected text, as well as open, save, and export files. The application also includes a dark mode toggle and an auto-save functionality that saves the text content every minute.
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/ 6

# ui/main_window.

py

import os

import json

from PyQt5.QtWidgets import (

QMainWindow, QWidget, QTextEdit, QListWidget, QPushButton,

QVBoxLayout, QHBoxLayout, QFileDialog, QAction, QMessageBox,


QLineEdit

from PyQt5.QtCore import Qt, QTimer

from database.models import session, Tag

class MainWindow(QMainWindow):

def __init__(self):

super().__init__()

self.setWindowTitle("NVivo Clone")

self.resize(900, 600)

self.is_dark_mode = False

self.setup_ui()

self.create_menu()

self.load_tags()

self.start_auto_save()

def setup_ui(self):

self.text_edit = QTextEdit()

self.tag_list = QListWidget()
self.tag_list.itemClicked.connect(self.apply_tag)

self.tag_input = QLineEdit()

self.tag_input.setPlaceholderText("Enter new tag")

self.add_tag_btn = QPushButton("Add Tag")

self.add_tag_btn.clicked.connect(self.add_tag)

self.search_bar = QLineEdit()

self.search_bar.setPlaceholderText("Search tags...")

self.search_bar.textChanged.connect(self.filter_tags)

tag_layout = QVBoxLayout()

tag_layout.addWidget(self.search_bar)

tag_layout.addWidget(self.tag_list)

tag_layout.addWidget(self.tag_input)

tag_layout.addWidget(self.add_tag_btn)

main_layout = QHBoxLayout()

main_layout.addLayout(tag_layout, 1)

main_layout.addWidget(self.text_edit, 3)

container = QWidget()

container.setLayout(main_layout)

self.setCentralWidget(container)

def create_menu(self):

menubar = self.menuBar()
file_menu = menubar.addMenu("File")

open_action = QAction("Open", self)

open_action.triggered.connect(self.open_file)

save_action = QAction("Save", self)

save_action.triggered.connect(self.save_file)

export_action = QAction("Export Tags", self)

export_action.triggered.connect(self.export_tags)

file_menu.addAction(open_action)

file_menu.addAction(save_action)

file_menu.addAction(export_action)

view_menu = menubar.addMenu("View")

theme_action = QAction("Toggle Theme", self)

theme_action.triggered.connect(self.toggle_theme)

view_menu.addAction(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 load_tags(self):

self.tag_list.clear()

tags = session.query(Tag).all()

for tag in tags:

self.tag_list.addItem(tag.name)
def add_tag(self):

tag_name = self.tag_input.text().strip()

if tag_name:

if not session.query(Tag).filter_by(name=tag_name).first():

new_tag = Tag(name=tag_name)

session.add(new_tag)

session.commit()

self.tag_list.addItem(tag_name)

self.tag_input.clear()

else:

QMessageBox.information(self, "Tag Exists", "This tag already


exists.")

def apply_tag(self, item):

cursor = self.text_edit.textCursor()

selected_text = cursor.selectedText()

if selected_text:

tagged_text = f"[{item.text()}:{selected_text}]"

cursor.insertText(tagged_text)

else:

QMessageBox.warning(self, "No Selection", "Please select text to


tag.")

def filter_tags(self, text):

for index in range(self.tag_list.count()):

item = self.tag_list.item(index)
item.setHidden(text.lower() not in item.text().lower())

def open_file(self):

path, _ = QFileDialog.getOpenFileName(self, "Open File", "", "Text Files


(*.txt)")

if path:

with open(path, 'r', encoding='utf-8') as file:

self.text_edit.setText(file.read())

def save_file(self):

path, _ = QFileDialog.getSaveFileName(self, "Save 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):

path, _ = QFileDialog.getSaveFileName(self, "Export Tags", "", "JSON


Files (*.json)")

if path:

tags = [tag.name for tag in session.query(Tag).all()]

with open(path, 'w', encoding='utf-8') as file:

json.dump(tags, file)

def toggle_theme(self):

self.is_dark_mode = not self.is_dark_mode

palette = self.palette()

if self.is_dark_mode:
self.setStyleSheet("""

QWidget { background-color: #2b2b2b; color: #ffffff; }

QLineEdit, QTextEdit { background-color: #3c3c3c; color: #ffffff; }

QListWidget { background-color: #3c3c3c; color: #ffffff; }

""")

else:

self.setStyleSheet("")

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) # 60 seconds

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