Menu

[8f66b6]: / SearchPageTabWidget.py  Maximize  Restore  History

Download this file

374 lines (328 with data), 17.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# -*- coding: utf-8 -*-
"""
Copyright (C) 2011 Oliver Tengler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import sys
from typing import List
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, QTimer
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QLabel, QAction, QToolTip, QPushButton
from widgets.LeaveLastTabWidget import LeaveLastTabWidget
from widgets.AnimatedProgressWidget import AnimatedProgressWidget
from tools import FileTools
from dialogs import StackTraceMessageBox
from dialogs.UserHintDialog import ButtonType,hintWouldBeShown,showUserHint
from dialogs.SettingsDialog import SettingsDialog
from SearchPage import SearchPage
import AppConfig
from fulltextindex import IndexConfiguration
import UpdateIndex
userHintInitialSetup= """
<p align='justify'>There are no search locations defined so far. </p>
<p align='justify'>Would you like to open the settings dialog and create a first search location now?</p>
"""
userHintDarkTheme="""
<p align='justify'>There is a dark theme available now. It can be selected from the options dialog.</p>
<p align='justify'>Would you like to give it a try?</p>
"""
class SearchPageTabWidget (LeaveLastTabWidget):
configChanged = pyqtSignal(list)
requestWindowTitleChange = pyqtSignal(str)
def __init__(self, parent:QWidget=None) -> None:
self.buttonSettings: QPushButton
self.buttonUpdate: QPushButton
self.indexOfUpdateButton:int
self.buttonHelp: QPushButton
self.buttonAbout: QPushButton
self.labelUpdate: QLabel
super().__init__(parent)
self.setNewTabButtonText(self.tr("New search"))
self.setPrototypeForNewTab(SearchPage, self.tr("Search"))
self.addNewTab()
# Add new tab (QKeySequence.AddTab is the same as Qt.CTRL + Qt.Key_T)
self.actionNewTab = QAction(self, shortcut=QKeySequence.AddTab, triggered= self.addNewTab)
self.addAction(self.actionNewTab)
# Close current tab
self.actionRemoveCurrentTab = QAction(self, shortcut=Qt.CTRL + Qt.Key_W, triggered= self.removeCurrentTab)
self.addAction(self.actionRemoveCurrentTab)
# Open settings
self.actionOpenSettings = QAction(self, shortcut=Qt.CTRL + Qt.Key_S, triggered= self.openSettings)
self.addAction(self.actionOpenSettings)
self.actionTab1 = QAction(self, shortcut=Qt.ALT + Qt.Key_1, triggered= self.activateTab1)
self.addAction(self.actionTab1)
self.actionTab2 = QAction(self, shortcut=Qt.ALT + Qt.Key_2, triggered= self.activateTab2)
self.addAction(self.actionTab2)
self.actionTab3 = QAction(self, shortcut=Qt.ALT + Qt.Key_3, triggered= self.activateTab3)
self.addAction(self.actionTab3)
self.actionTab4 = QAction(self, shortcut=Qt.ALT + Qt.Key_4, triggered= self.activateTab4)
self.addAction(self.actionTab4)
self.actionTab5 = QAction(self, shortcut=Qt.ALT + Qt.Key_5, triggered= self.activateTab5)
self.addAction(self.actionTab5)
self.actionTab6 = QAction(self, shortcut=Qt.ALT + Qt.Key_6, triggered= self.activateTab6)
self.addAction(self.actionTab6)
self.indexUpdateTimer: QTimer = None
self.indexTriggerPath = os.path.join (AppConfig.userDataPath (), "TriggerUpdate")
self.handleUncleanShutdown()
# A list of index names which are currently disabled because the update is running
self.disabledIndexes: List[str] = []
# If the index update is still running this will start the timer which watches for it to finish
self.__watchForIndexUpdate()
# Wait a little for the main window to display, then ask user for initial setup
QTimer.singleShot (500, self.initialSetup)
def handleUncleanShutdown(self) -> None:
"""
If UpdateIndex.exe crashes or is terminated by the user some files are
left behind which cause CodeBeagle to think that there is an update index running.
Clean this up.
"""
UpdateIndex.handleUncleanShutdown(self.indexTriggerPath, removeTriggerFiles = True)
@pyqtSlot()
def activateTab1(self) -> None:
self.setCurrentIndex(0)
@pyqtSlot()
def activateTab2(self) -> None:
self.setCurrentIndex(1)
@pyqtSlot()
def activateTab3(self) -> None:
self.setCurrentIndex(2)
@pyqtSlot()
def activateTab4(self) -> None:
self.setCurrentIndex(3)
@pyqtSlot()
def activateTab5(self) -> None:
self.setCurrentIndex(4)
@pyqtSlot()
def activateTab6(self) -> None:
self.setCurrentIndex(5)
# Register a button in the corner widget to open the settings dialog. This function is called by the base class.
def addWidgetsToCornerWidget (self, hbox: QHBoxLayout) -> None:
super ().addWidgetsToCornerWidget(hbox)
self.buttonSettings = self.addButtonToCornerWidget (hbox, self.tr("Settings"), "Settings.png", self.openSettings)
self.buttonUpdate = self.addButtonToCornerWidget (hbox, self.tr("Update index"), "Update.png", self.updateIndex)
self.indexOfUpdateButton = hbox.count()-1
self.buttonHelp = self.addButtonToCornerWidget (hbox, self.tr("Help"), "Help.png", self.openHelp)
self.buttonAbout = self.addButtonToCornerWidget (hbox, self.tr("About"), "CodeBeagle.png", self.openAbout)
self.labelUpdate = None
# The settings allow to configure search locations.
@pyqtSlot()
def openSettings(self, createInitialLocation: bool=False, locationToAdd: IndexConfiguration.IndexConfiguration=None) -> None:
try:
config = AppConfig.userConfig()
globalConfig = AppConfig.globalConfig()
except:
self.userConfigFailedToLoadMessage()
else:
searchLocations = IndexConfiguration.readConfig(config)
globalSearchLocations = IndexConfiguration.readConfig(globalConfig)
settingsDlg = SettingsDialog(self, searchLocations, globalSearchLocations, config)
settingsDlg.updateChangedIndexes.connect(self.triggerIndexUpdate)
if createInitialLocation:
settingsDlg.addLocation()
if locationToAdd:
settingsDlg.addExistingLocation(locationToAdd)
if settingsDlg.exec():
# Refresh config
AppConfig.refreshConfig()
searchLocations = IndexConfiguration.readConfig(AppConfig.appConfig())
self.configChanged.emit(searchLocations)
@pyqtSlot(str)
def addSearchLocationFromPath (self, directory: str) -> None:
"""This is called when a directory is dropped on the application. This is a shortcut to create a search location."""
ext = FileTools.getMostCommonExtensionInDirectory (directory)
location = IndexConfiguration.IndexConfiguration(self.tr("Search") + " '" + directory + "'", ext, directory, "", "", IndexConfiguration.IndexMode.NoIndexWanted)
self.openSettings (locationToAdd=location)
@pyqtSlot()
def updateIndex(self) -> None:
try:
config = AppConfig.userConfig()
except:
self.userConfigFailedToLoadMessage()
else:
searchLocations = IndexConfiguration.readConfig(config)
displayNames = [conf.displayName() for conf in searchLocations if conf.generatesIndex()]
from dialogs.CheckableItemsDialog import CheckableItemsDialog
updateDialog = CheckableItemsDialog(self.tr("Choose indexes to update"), True, self)
for name in displayNames:
updateDialog.addItem(name)
updateDialog.checkAll(True)
if updateDialog.exec():
# The items returned are touples: (index,value)
updateDisplayNames = [name for i, name in updateDialog.checkedItems()]
self.triggerIndexUpdate (updateDisplayNames)
@pyqtSlot()
def openHelp(self) -> None:
from dialogs.HelpViewerDialog import HelpViewerDialog
helpDialog = HelpViewerDialog(self)
helpDialog.setAttribute(Qt.WA_DeleteOnClose)
helpDialog.showFile("help.html")
helpDialog.show()
@pyqtSlot()
def openAbout(self) -> None:
from dialogs.AboutDialog import AboutDialog
aboutDialog = AboutDialog(self)
aboutDialog.exec()
@pyqtSlot()
def triggerIndexUpdate (self, updateDisplayNames: List[str]) -> None:
try:
self.__triggerIndexUpdate (updateDisplayNames)
except:
self.failedToUpdateIndexesMessage()
def __triggerIndexUpdate (self, updateDisplayNames: List[str]) -> None:
r"""
Check which indexes should be updated and trigger an asynchronous update
This works by putting an file with the name of the index config group into %APPDATA%\CodeBeagle\IndexUpdate.
It is picked up by UpdateIndex.py which has a special mode for this task.
"""
if not os.path.isdir(self.indexTriggerPath):
os.makedirs(self.indexTriggerPath)
for name in updateDisplayNames:
open(os.path.join(self.indexTriggerPath, FileTools.removeInvalidFileChars(name)), "w").close()
# Now start UpdateIndex.py as a subprocess. If we are running as "CodeBeagle.exe" then start "UpdateIndex.exe" instead
import subprocess
runningInInterpreter = os.path.basename(sys.executable).lower().startswith("python")
args = ["--jobmode", self.indexTriggerPath, "--config", AppConfig.userConfigFileName()]
if runningInInterpreter:
subprocess.Popen ([sys.executable, "UpdateIndex.py"] + args)
else:
if os.path.exists("UpdateIndex.exe"):
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE
subprocess.Popen (["UpdateIndex.exe"] + args, startupinfo=si)
else:
raise RuntimeError("UpdateIndex.exe not found")
self.__watchForIndexUpdate()
def __watchForIndexUpdate(self) -> None:
"""Check regularily if the update finished."""
running = self.__indexUpdateRunning()
self.__maintainRunningIndexUpdates(running)
if running:
self.__showIndexUpdateInProgress(True)
if not self.indexUpdateTimer:
self.indexUpdateTimer = QTimer(self)
self.indexUpdateTimer.timeout.connect(self.__checkIndexUpdateProgress)
self.indexUpdateTimer.start(2000)
def __checkIndexUpdateProgress (self) -> None:
running = self.__indexUpdateRunning()
self.__maintainRunningIndexUpdates(running)
if not running:
self.indexUpdateTimer.stop()
self.__showIndexUpdateInProgress(False)
self.__informAboutIndexUpdate("Index update finshed")
def __indexUpdateRunning (self) -> List[str]:
try:
files = os.listdir(self.indexTriggerPath)
return files
except:
return []
def __maintainRunningIndexUpdates(self, running: List[str]) -> None:
"""Informs all search pages which search locations are currently not available."""
if len(running) != len(self.disabledIndexes):
self.disabledIndexes = running
searchLocations = IndexConfiguration.readConfig(AppConfig.appConfig())
availableLocations = []
for location in searchLocations:
if not FileTools.removeInvalidFileChars(location.displayName()) in self.disabledIndexes:
availableLocations.append (location)
self.configChanged.emit(availableLocations)
else:
self.disabledIndexes = running
def __informAboutIndexUpdate(self, text: str) -> None:
pos = self.labelUpdate.parent().mapToGlobal(self.labelUpdate.pos())
pos.setX(pos.x())
QToolTip.showText (pos, text, self)
def __addAnimatedUpdateLabel (self, hbox: QHBoxLayout, text: str) -> QWidget:
widget = AnimatedProgressWidget (self, text)
widget.hide()
hbox.insertWidget(self.indexOfUpdateButton, widget)
return widget
def __showIndexUpdateInProgress (self, bInProgress: bool) -> None:
if not self.labelUpdate:
self.labelUpdate = self.__addAnimatedUpdateLabel (self.cornerWidgetLayout(), self.tr("Update running..."))
if bInProgress:
self.buttonUpdate.hide()
self.labelUpdate.show()
self.buttonSettings.setEnabled(False)
self.labelUpdate.start()
else:
self.labelUpdate.stop()
self.buttonUpdate.show()
self.labelUpdate.hide()
self.buttonSettings.setEnabled(True)
def newTabAdded(self, prevTabWidget:SearchPage, newTabWidget:SearchPage) -> None:
"""
This is called by the base class when a new tab is added. We use this to connect the request for a new search
to open up in a new tab.
"""
newTabWidget.newSearchRequested.connect (self.searchInNewTab)
newTabWidget.searchFinished.connect (self.changeTabName)
newTabWidget.documentShown.connect (self.requestWindowTitleChange)
self.requestWindowTitleChange.emit("")
newTabWidget.ui.sourceViewer.directoryDropped.connect(self.addSearchLocationFromPath)
self.configChanged.connect (newTabWidget.reloadConfig)
# Initially reload the config to pass the current search locations to the search page
newTabWidget.reloadConfig(IndexConfiguration.readConfig(AppConfig.appConfig()))
# Copy search location from previous search page
if prevTabWidget:
newTabWidget.setCurrentSearchLocation(prevTabWidget.currentConfigName)
@pyqtSlot(str, str)
def searchInNewTab (self, text: str, searchLocationName: str) -> None:
searchPage = self.addNewTab ()
searchPage.setCurrentSearchLocation(searchLocationName)
searchPage.searchForText(text)
@pyqtSlot(QWidget, str)
def changeTabName (self, searchPage: SearchPage, text: str) -> None:
index = self.indexOf(searchPage)
if -1 != index:
if text:
self.setTabText(index, text)
else:
self.setTabText(index, self.tr("Search"))
@pyqtSlot()
def initialSetup(self) -> None:
if hintWouldBeShown("noLocations"):
locations = IndexConfiguration.readConfig(AppConfig.appConfig())
if not locations:
text = self.tr(userHintInitialSetup)
res = showUserHint (self, "noLocations", self.tr("Initial setup"), text, ButtonType.Yes, True, ButtonType.No)
if res == ButtonType.Yes:
self.openSettings(createInitialLocation=True)
if AppConfig.appConfig().theme != AppConfig.darkTheme:
text = self.tr(userHintDarkTheme)
if showUserHint (self, "darkTheme", self.tr("Dark theme"), text, ButtonType.Yes, True, ButtonType.No) == ButtonType.Yes:
AppConfig.appConfig().theme = AppConfig.darkTheme
AppConfig.saveUserConfig(AppConfig.appConfig())
self.__restartCodeBeagle()
sys.exit(0)
def __restartCodeBeagle(self) -> None:
import subprocess
runningInInterpreter = os.path.basename(sys.executable).lower().startswith("python")
if runningInInterpreter:
subprocess.Popen ([sys.executable, "CodeBeagle.pyw"])
else:
if os.path.exists("CodeBeagle.exe"):
si = subprocess.STARTUPINFO()
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
si.wShowWindow = subprocess.SW_HIDE
subprocess.Popen (["CodeBeagle.exe"], startupinfo=si)
else:
raise RuntimeError("CodeBeagle.exe not found")
def userConfigFailedToLoadMessage(self) -> None:
StackTraceMessageBox.show(self,
self.tr("Failed to load user config"),
self.tr("The user config file could not be loaded from the user profile"))
def failedToUpdateIndexesMessage(self) -> None:
StackTraceMessageBox.show(self,
self.tr("Error during index update"),
self.tr("The update process failed to update the desired indexes"))
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.