Skip to content

Commit

Permalink
* Added checkbox to turn off the log view in case you're using an ext…
Browse files Browse the repository at this point in the history
…ernal log viewer
  • Loading branch information
nielsvaes committed Mar 30, 2024
1 parent e6796ad commit 0928f95
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 84 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.4.6
* Added checkbox in the View menu to turn off the log view in case you're using an external log viewer

## 1.4.5
* Copying of hook file is now an option in the settings that defaults to False

Expand Down
6 changes: 4 additions & 2 deletions dcs_code_injector/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os

VERSION = "1.4.5"
ICON = os.path.join(os.path.dirname(__file__), "ui", "icons", "icon.png")
VERSION = "1.4.6"
ICON = os.path.join(os.path.dirname(__file__), "ui", "icons", "icon.png")
LOGO = os.path.join(os.path.dirname(__file__), "ui", "icons", "logo.png")
UI_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ui")
6 changes: 2 additions & 4 deletions dcs_code_injector/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
from ez_settings import EZSettings
from qt_material import apply_stylesheet

from . import ICON
from . import utils
from . import ICON, utils, UI_DIR, LOGO
from .dcs_code_injector_window import CodeInjectorWindow
from .constants import sk, DEFAULT_HIGHLIGHTING_RULES

SETTINGS_DIR = os.path.join(os.path.expanduser('~'),'Documents', "dcs_code_injector")
SETTINGS_PATH = os.path.join(SETTINGS_DIR, "settings.json")
UI_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "ui")
SPLASH_DIR = os.path.join(UI_DIR, "splashscreens")

application = QApplication()
Expand Down Expand Up @@ -69,7 +67,7 @@ def main():
splash = QSplashScreen(QPixmap(splashscreens[random.randint(0, len(splashscreens) - 1)]))
splash.show()

application.setWindowIcon(QIcon(ICON))
application.setWindowIcon(QIcon(LOGO))
application.processEvents()

theme = EZSettings().get(sk.theme, sk.theme_material_neon)
Expand Down
2 changes: 2 additions & 0 deletions dcs_code_injector/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class SettingConstants:
theme_fusion_dark = "Fusion Dark"
theme = "theme"
copy_hook_on_startup = "copy_hook_on_startup"
update_log_view = "update_log_view"


sk = SettingConstants

Expand Down
89 changes: 17 additions & 72 deletions dcs_code_injector/dcs_code_injector_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
from PySide6.QtGui import *
from PySide6.QtCore import *

import os
import json
import pathlib
import socket
import time

from functools import partial
from ez_settings import EZSettings
from datetime import datetime, timedelta
from pygtail import Pygtail

from .settings_dialog import SettingsDialog
from .version_dialog import AboutDialog
from .code_editor import CodeTextEdit
from .favorites import FavoritesWidget
from .log_view import LogView
from .log_highlighter import LogHighlighter
from .ui.dcs_code_injector_window_ui import Ui_MainWindow

from .constants import sk
Expand Down Expand Up @@ -52,83 +48,40 @@ def __init__(self):

self.about_dialog = AboutDialog()

self.last_log_file_size = 0
# self.last_log_file_size = 0

self.txt_log = LogView()
self.txt_log.showSettings.connect(self.show_settings)
self.txt_log.playErrorSound.connect(self.play_error_sound)
self.txt_log_layout.addWidget(self.txt_log)
self.log_file = EZSettings().get(sk.log_file, "")
self.previous = []
LogHighlighter(self.txt_log.document())
self.read_log()

self.connect_ui_signals()
self.load()

# self.update_code_views_font()

self.timer = QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.read_log)
self.timer.start()

self.back_up_settings_file()
if EZSettings().get(sk.copy_hook_on_startup, False):
self.copy_hook_file()

self.show()
self.init_done = True


def read_log(self):
"""
Reads the log file and updates the log view.
Handles the time shifting for the log entries.
"""

if not os.path.isfile(self.log_file):
self.show_settings()
self.log_file = EZSettings().get(sk.log_file, "")

if not self.init_done:
with open(self.log_file, "r") as readfile:
self.add_text_to_log(readfile.read())

for line in Pygtail(self.log_file):
try:
line = line.replace(" ", " ")
time_str = line[0:22]
time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")
shift_hours = EZSettings().get(sk.shift_hours, 0)
time += timedelta(hours=shift_hours)
shifted_time_str = time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]
line = line.replace(time_str, shifted_time_str)

diff = datetime.now() - time
if ("mission script error" in line.lower() and
diff.total_seconds() > 0.7 and
self.init_done and
EZSettings().get(sk.play_sound_on_mission_scripting_error, True)):
self.play_error_sound()

self.add_text_to_log(line)
except ValueError as err:
self.add_text_to_log(line)

def connect_ui_signals(self):
"""
Connects the UI signals to their respective slots.
"""

self.tab_widget.tabBarDoubleClicked.connect(self.rename_tab)
self.tab_widget.tabCloseRequested.connect(self.close_tab)

self.action_settings.triggered.connect(self.show_settings)
self.action_back_up_settings_file.triggered.connect(self.back_up_settings_file)
self.action_clear_log.triggered.connect(self.clear_log)
self.action_clear_log.triggered.connect(self.txt_log.clear)
self.action_search.triggered.connect(self.txt_log.toggle_search)
self.action_add_new_tab.triggered.connect(lambda _: self.add_new_tab(name="UNNAMED", code="-- add code here"))
self.action_copy_hook_file.triggered.connect(self.copy_hook_file)
self.action_increase_code_font_size.triggered.connect(lambda _: self.adjust_font_size(self.tab_widget.currentWidget(), True))
self.action_decrease_code_font_size.triggered.connect(lambda _: self.adjust_font_size(self.tab_widget.currentWidget(), False))
self.action_log_view_enabled.toggled.connect(self.enabled_log_view)
self.action_pick_log_font.triggered.connect(lambda _: self.pick_font("log"))
self.action_increase_log_font_size.triggered.connect(lambda _: self.adjust_font_size(self.txt_log, True))
self.action_decrease_log_font_size.triggered.connect(lambda _: self.adjust_font_size(self.txt_log, False))
Expand Down Expand Up @@ -166,13 +119,6 @@ def adjust_font_size(self, widget, increase):
self.txt_log.setStyleSheet(f"font: {size}pt 'Courier New';")
EZSettings().set(sk.log_font_size, size)

def clear_log(self):
"""
"Clears" the log view by just adding 60 newlines to push everything up
"""

self.add_text_to_log("\n" * 60)

def connect_favorite_button(self, favorite_button):
"""
Connects the favorite button to its respective slots.
Expand Down Expand Up @@ -239,6 +185,14 @@ def rename_tab(self):
self.tab_widget.setTabText(self.tab_widget.currentIndex(), name)
self.save_code()

def enabled_log_view(self):
enable = self.action_log_view_enabled.isChecked()
self.txt_log.enable_updating(enable)
if enable:
self.splitter.setSizes([500, 200])
else:
self.splitter.setSizes([0, 4000])

def load(self):
"""
Loads the settings and initializes the UI accordingly.
Expand All @@ -250,6 +204,8 @@ def load(self):
if setting.startswith("btn_"):
self.favorites_widget.add_new_button(setting.replace("btn_", ""), EZSettings().get(setting))

self.action_log_view_enabled.setChecked(EZSettings().get(sk.update_log_view, True))

def save_code(self):
"""
Saves the code in the current tab to the settings.
Expand All @@ -276,18 +232,7 @@ def add_code_to_log(self, text, header_text="CODE BLOCK"):
line_number += 1
numbered_lines.append(f"\n------------------ /{header_text} -------------------\n")
complete_text = "\n".join(numbered_lines)
self.add_text_to_log(complete_text)

def add_text_to_log(self, complete_text):
"""
Adds the given text to the log view.
:param complete_text: <str> the text to be added to the log
"""

self.txt_log.moveCursor(QTextCursor.End)
self.txt_log.insertPlainText(complete_text)
self.txt_log.verticalScrollBar().setValue(self.txt_log.verticalScrollBar().maximum())
self.txt_log.add_text(complete_text)

def send_code(self, code):
self.add_code_to_log(code)
Expand Down
77 changes: 74 additions & 3 deletions dcs_code_injector/log_view.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,59 @@
from datetime import datetime, timedelta

from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *
from PySide6.QtCore import Qt

import os

from pygtail import Pygtail

from .log_highlighter import LogHighlighter
from .ui.dcs_code_injector_search_ui import Ui_Form
from .constants import sk

from ez_settings import EZSettings

class LogView(QPlainTextEdit):
showSettings = Signal()
playErrorSound = Signal()
def __init__(self):
"""
Constructor for the LogView class.
"""
self.init_done = False

super().__init__()
self.font = EZSettings().get(sk.log_font, sk.default_font)
self.font_size = EZSettings().get(sk.log_font_size, 10)
self.__update_font()

# make search widget
self.search_widget = SearchBox()
self.search_widget.txt_search.returnPressed.connect(self.search_text)

self.last_search_position = 0

self.grid_layout = QGridLayout(self)
self.grid_layout.setContentsMargins(0, 10 , 15, 0)
self.grid_layout.addWidget(self.search_widget, 0, 0, 0, 0, Qt.AlignTop | Qt.AlignRight)


self.last_search_position = 0

self.log_file = EZSettings().get(sk.log_file, "")
if not os.path.isfile(self.log_file):
self.showSettings.emit()
self.log_file = EZSettings().get(sk.log_file, "")

self.timer = QTimer()
self.timer.setInterval(500)
self.timer.timeout.connect(self.read_log)
if EZSettings().get(sk.update_log_view, True):
self.timer.start()

LogHighlighter(self.document())

self.setReadOnly(True)
self.init_done = True

def set_font(self, font):
self.font = font
Expand All @@ -39,6 +65,13 @@ def set_font_size(self, font_size):
EZSettings().set(sk.log_font_size, font_size)
self.__update_font()

def enable_updating(self, value):
EZSettings().set(sk.update_log_view, value)
if value:
self.timer.start()
else:
self.timer.stop()

def toggle_search(self):
"""
Toggles the visibility of the search widget.
Expand Down Expand Up @@ -66,6 +99,44 @@ def search_text(self):
else:
self.last_search_position = 0

def add_text(self, complete_text):
self.moveCursor(QTextCursor.End)
self.insertPlainText(complete_text)
self.verticalScrollBar().setValue(self.verticalScrollBar().maximum())

def clear(self):
self.add_text("\n" * 60)

def read_log(self):
if not os.path.isfile(self.log_file):
self.showSettings.emit()
self.log_file = EZSettings().get(sk.log_file, "")

if not self.init_done:
with open(self.log_file, "r") as readfile:
self.add_text(readfile.read())

for line in Pygtail(self.log_file):
try:
line = line.replace(" ", " ")
time_str = line[0:22]
time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S.%f")
shift_hours = EZSettings().get(sk.shift_hours, 0)
time += timedelta(hours=shift_hours)
shifted_time_str = time.strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]
line = line.replace(time_str, shifted_time_str)

diff = datetime.now() - time
if ("mission script error" in line.lower() and
diff.total_seconds() > 0.7 and
self.init_done and
EZSettings().get(sk.play_sound_on_mission_scripting_error, True)):
self.playErrorSound.emit()

self.add_text(line)
except ValueError as err:
self.add_text(line)

def __update_font(self):
self.setStyleSheet(f"font: {self.font_size}pt '{self.font}';")

Expand Down
6 changes: 6 additions & 0 deletions dcs_code_injector/ui/dcs_code_injector_window_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def setupUi(self, MainWindow):
self.action_material_neon.setObjectName(u"action_material_neon")
self.action_fusion_dark = QAction(MainWindow)
self.action_fusion_dark.setObjectName(u"action_fusion_dark")
self.action_log_view_enabled = QAction(MainWindow)
self.action_log_view_enabled.setObjectName(u"action_log_view_enabled")
self.action_log_view_enabled.setCheckable(True)
self.action_log_view_enabled.setChecked(True)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.gridLayout = QGridLayout(self.centralwidget)
Expand Down Expand Up @@ -114,6 +118,7 @@ def setupUi(self, MainWindow):
self.menuTools.addAction(self.action_search)
self.menuTools.addSeparator()
self.menuTools.addAction(self.action_copy_hook_file)
self.menuView.addAction(self.action_log_view_enabled)
self.menuView.addAction(self.action_increase_log_font_size)
self.menuView.addAction(self.action_decrease_log_font_size)
self.menuView.addAction(self.action_pick_log_font)
Expand Down Expand Up @@ -177,6 +182,7 @@ def retranslateUi(self, MainWindow):
self.action_pick_code_font.setText(QCoreApplication.translate("MainWindow", u"Pick code font", None))
self.action_material_neon.setText(QCoreApplication.translate("MainWindow", u"Material Neon", None))
self.action_fusion_dark.setText(QCoreApplication.translate("MainWindow", u"Fusion Dark", None))
self.action_log_view_enabled.setText(QCoreApplication.translate("MainWindow", u"Log view enabled", None))
self.menuFile.setTitle(QCoreApplication.translate("MainWindow", u"File", None))
self.menuTools.setTitle(QCoreApplication.translate("MainWindow", u"Tools", None))
self.menuView.setTitle(QCoreApplication.translate("MainWindow", u"View", None))
Expand Down
12 changes: 12 additions & 0 deletions dcs_code_injector/ui/dcs_code_injector_window_ui.ui
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<addaction name="action_material_neon"/>
<addaction name="action_fusion_dark"/>
</widget>
<addaction name="action_log_view_enabled"/>
<addaction name="action_increase_log_font_size"/>
<addaction name="action_decrease_log_font_size"/>
<addaction name="action_pick_log_font"/>
Expand Down Expand Up @@ -205,6 +206,17 @@
<string>Fusion Dark</string>
</property>
</action>
<action name="action_log_view_enabled">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Log view enabled</string>
</property>
</action>
</widget>
<resources/>
<connections/>
Expand Down
Binary file added dcs_code_injector/ui/icons/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0928f95

Please sign in to comment.