def main(): """The main routine.""" # Define the names of the organization and the application # The value is used by the QSettings class when it is constructed using # the empty constructor. This saves having to repeat this information # each time a QSettings object is created. # The default scope is QSettings::UserScope QCoreApplication.setOrganizationName("labsquare") QCoreApplication.setApplicationName("cutevariant") QCoreApplication.setApplicationVersion(__version__) # Process command line arguments app = QApplication(sys.argv) process_arguments(app) # Load app styles load_styles(app) # # Uncomment those line to clear settings # settings = QSettings() # settings.clear() # Set icons set setFontPath(cm.FONT_FILE) # Translations load_translations(app) # debug settings # from cutevariant.gui.settings import * # w = SettingsWidget() # w.show() # Splash screen splash = QSplashScreen() splash.setPixmap(QPixmap(cm.DIR_ICONS + "splash.png")) splash.showMessage(f"Version {__version__}") splash.show() app.processEvents() # Drop settings if old version settings = QSettings() settings_version = settings.value("version", None) if settings_version is None or parse_version( settings_version) < parse_version(__version__): settings.clear() settings.setValue("version", __version__) # Display w = MainWindow() # STYLES = cm.DIR_STYLES + "frameless.qss" # with open(STYLES,"r") as file: # w.setStyleSheet(file.read()) w.show() splash.finish(w) app.exec_()
def test_reload(self, temp_settings): app, home = temp_settings settings = QSettings() settings.clear() settings.setValue('instrument', 123) settings.beginWriteArray('exercises') settings.setValue('sample1', ['1', '2', '3']) settings.setValue('sample2', ['4', '5', '6']) settings.endArray() app.reload_settings() assert app.instrument == 123 assert app.exercises == [['sample1', ['1', '2', '3']], ['sample2', ['4', '5', '6']]]
def clearSettings(self): """Remove all settings""" settings = QSettings(self.companyName, self.toolName) settings.clear()
class Widget(QWidget): def __init__(self): QWidget.__init__(self) self.row = 0 self.coins_sum = 0 self.settings = QSettings('NoneCompany', 'freebot') VBoxLayout = QVBoxLayout() # Right HBoxLayout = QHBoxLayout() # Left self.create_form() self.set_placeholders() self.define_table() FormLayout = QHBoxLayout() FormLayout.addWidget(self.form_login) FormLayout.addWidget(self.form_password) FormLayout.addWidget(self.form_proxy) self.create_buttons() FromButtonsLayout = QHBoxLayout() FromButtonsLayout.addWidget(self.add_button) FromButtonsLayout.addWidget(self.clear_button) self.series = QtCharts.QLineSeries() self.chart_view = QtCharts.QChartView() self.chart_view.setRenderHint(QPainter.Antialiasing) VBoxLayout.addWidget(self.form_key) VBoxLayout.addLayout(FormLayout) VBoxLayout.addLayout(FromButtonsLayout) VBoxLayout.addWidget(self.chart_view) VBoxLayout.addWidget(self.start_button) HBoxLayout.addWidget(self.table) HBoxLayout.addLayout(VBoxLayout) self.setLayout(HBoxLayout) self.update_chart([time(), 0], True) self.create_connections() def set_placeholders(self): self.form_login.setPlaceholderText('login') self.form_password.setPlaceholderText('password') self.form_proxy.setPlaceholderText('proxy') self.form_key.setPlaceholderText('API-key rucaptcha.com') def create_form(self): self.form_login = QLineEdit() self.form_password = QLineEdit() self.form_proxy = QLineEdit() self.form_key = QLineEdit() def create_buttons(self): self.add_button = QPushButton('add') self.clear_button = QPushButton('clear') self.start_button = QPushButton('start') def create_connections(self): self.add_button.clicked.connect(self.fillin_table) self.start_button.clicked.connect(self.init_threads) self.clear_button.clicked.connect(self.clear_table) def define_table(self): self.table = QTableWidget() self.table.setColumnCount(len(CONSTANTS.TABLE_COLUMNS)) self.table.setHorizontalHeaderLabels(CONSTANTS.TABLE_COLUMNS) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) if self.settings.contains('key'): self.form_key.setText(self.get_settings('key')) if self.settings.contains('table'): for row in literal_eval(self.get_settings('table')): self.add_account(*row, False) def set_settings(self, key, value): if key == 'table': if self.settings.contains('table'): table = literal_eval(self.get_settings('table')) table.append(value) value = table else: value = [value] self.settings.setValue(key, str(value)) def get_settings(self, key): return self.settings.value(key) @Slot(int, list) def change_column_color(self, row, RGB): for col in range(len(CONSTANTS.TABLE_COLUMNS)): self.table.item(row, col).setBackground(QColor(*RGB)) @Slot(int, str) def change_column_text(self, row, text): self.table.item(row, 3).setText(text) @Slot() def clear_table(self): self.settings.clear() self.table.setRowCount(0) self.row = 0 @Slot() def fillin_table(self, settings=False): if self.check_form_state(): return self.add_account(self.form_login.text(), self.form_password.text(), self.form_proxy.text()) self.form_login.clear() self.form_proxy.clear() self.form_password.clear() @Slot(list) def update_chart(self, data, init=False): self.coins_sum += data[1] MStime_now = float(QDateTime.currentDateTime().toMSecsSinceEpoch()) if init: self.series.append(MStime_now, self.coins_sum) for i in range(6): if i and not i % 2: self.series.append(MStime_now + (i * 1), self.coins_sum) else: self.series.append(MStime_now + (i * 1), 0.00000001) else: self.series.append(MStime_now, self.coins_sum) chart = QtCharts.QChart() chart.addSeries(self.series) if not init: chart.setAnimationOptions(QtCharts.QChart.SeriesAnimations) chart.setTitle('Progress') axisX = QtCharts.QDateTimeAxis() axisX.setTickCount(5) axisX.setFormat('HH:mm:ss') axisX.setTitleText('time') chart.addAxis(axisX, Qt.AlignBottom) self.series.attachAxis(axisX) axisY = QtCharts.QValueAxis() axisY.setTickCount(5) axisY.setLabelFormat('%.8f') axisY.setTitleText('coins') chart.addAxis(axisY, Qt.AlignLeft) self.series.attachAxis(axisY) self.chart_view.setChart(chart) @Slot() def init_threads(self): self.start_button.setEnabled(False) self.set_settings('key', self.form_key.text()) self.threads = [ Bridge(self, row, self.table.item(row, 0).text(), self.table.item(row, 1).text(), self.table.item(row, 2).text(), self.form_key.text()) for row in range(self.table.rowCount()) ] def check_form_state(self): return not self.form_login.text() \ or not self.form_password.text() \ or not self.form_proxy.text() def add_account(self, login, password, proxy, record=True): self.table.insertRow(self.row) self.table.setItem(self.row, 0, QTableWidgetItem(login)) self.table.setItem(self.row, 1, QTableWidgetItem(password)) self.table.setItem(self.row, 2, QTableWidgetItem(proxy)) self.table.setItem(self.row, 3, QTableWidgetItem('?')) if record: self.set_settings('key', self.form_key.text()) self.set_settings('table', [login, password, proxy]) self.row += 1
def settings_clear(app: QApplication, settings: QSettings, main_window: QMainWindow): set_system_style(app, settings) initialize_ui_default(main_window) settings.clear()
def reset(self): settings = QSettings() settings.clear() self.read_settings()
class SettingsDialog(QtWidgets.QDialog): validator = QRegExpValidator(QRegExp("[A-Za-z0-9_]{6,16}")) def __init__(self, parent=None): super(SettingsDialog, self).__init__(parent) self.settings = QSettings( QStandardPaths.writableLocation(QStandardPaths.ConfigLocation) + "/settings.ini", QSettings.IniFormat) self.usernames = set() self.engines = set() self.newUsername = "" self.newEnginePath = "" self._loadSettings() self.mainLayout = QtWidgets.QFormLayout() self.usernameLineEdit = QtWidgets.QLineEdit(self.newUsername) self.usernameLineEdit.setPlaceholderText("Username (a-zA-Z0-9_)") self.usernameLineEdit.setMinimumHeight(35) self.usernameLineEdit.setValidator(self.validator) self.usernameLineEdit.textChanged.connect(self.validateFields) self.usernameLineEdit.selectAll() self.usernameLineEdit.setCompleter( QtWidgets.QCompleter(list(self.usernames), self)) self.engineEdit = _EngineEdit(self.newEnginePath) self.engineEdit.pathEdit.textChanged.connect(self.validateFields) self.engineEdit.pathEdit.selectAll() self.engineEdit.pathEdit.setCompleter( QtWidgets.QCompleter(list(self.engines), self)) self.resetButton = QtWidgets.QPushButton("Reset") self.resetButton.clicked.connect(self._reset) buttonBox = QtWidgets.QDialogButtonBox() self.okButton = buttonBox.addButton( "Ok", QtWidgets.QDialogButtonBox.AcceptRole) self.cancelButton = buttonBox.addButton( "Cancel", QtWidgets.QDialogButtonBox.RejectRole) self.okButton.clicked.connect(self._ok) self.cancelButton.clicked.connect(self.reject) self.mainLayout.setContentsMargins(0, 0, 0, 0) self.mainLayout.setAlignment(Qt.AlignBottom) self.mainLayout.addRow("Username", self.usernameLineEdit) self.mainLayout.addRow("Engine", self.engineEdit) self.mainLayout.addWidget(self.resetButton) self.mainLayout.addWidget(buttonBox) self.setLayout(self.mainLayout) self.validateFields() def _validateEnginePath(self, path): return QFile.exists(path) and QFileInfo(path).isFile() @Slot(str) def validateFields(self): usernameValid = self.validator.validate(self.usernameLineEdit.text(), 0) and \ 6 <= len(self.usernameLineEdit.text()) <= 16 pathValid = self._validateEnginePath(self.engineEdit.pathEdit.text()) if not usernameValid or not pathValid: if self.okButton.isEnabled(): self.okButton.setDisabled(True) else: if not self.okButton.isEnabled(): self.okButton.setDisabled(False) if not usernameValid: self.usernameLineEdit.setStyleSheet("border: 1px solid red;") else: self.usernameLineEdit.setStyleSheet("border: 1px solid green;") if not pathValid: self.engineEdit.pathEdit.setStyleSheet("border: 1px solid red;") else: self.engineEdit.pathEdit.setStyleSheet("border: 1px solid green;") def _loadSettings(self): i = self.settings.beginReadArray("usernames") currentUsername = "" for j in range(i): self.settings.setArrayIndex(j) currentUsername = self.settings.value("usernames", "") self.usernames.add(currentUsername) self.newUsername = currentUsername self.settings.endArray() i = self.settings.beginReadArray("engines") currentEngine = "" for j in range(i): self.settings.setArrayIndex(j) currentEngine = self.settings.value("engines", "") self.engines.add(currentEngine) self.newEnginePath = currentEngine self.settings.endArray() @Slot() def _reset(self): status = QtWidgets.QMessageBox.warning( self, "Reset", "Are you sure to reset the settings?", buttons=QtWidgets.QMessageBox.Cancel | QtWidgets.QMessageBox.Ok, defaultButton=QtWidgets.QMessageBox.Ok) if status == QtWidgets.QMessageBox.Ok: self.usernameLineEdit.clear() self.engineEdit.pathEdit.clear() self.usernames.clear() self.engines.clear() self.usernameLineEdit.setCompleter(QtWidgets.QCompleter()) self.engineEdit.pathEdit.setCompleter(QtWidgets.QCompleter()) self.settings.clear() self._loadSettings() @Slot() def _ok(self): if self.validator.validate(self.usernameLineEdit.text(), 0): self.newUsername = self.usernameLineEdit.text() else: QtWidgets.QMessageBox.critical(self, "Error", "Invalid username") self.reject() if self._validateEnginePath(self.engineEdit.pathEdit.text()): self.newEnginePath = self.engineEdit.pathEdit.text() else: QtWidgets.QMessageBox.critical( self, "Error", "The specified engine's path does not exist.") self.reject() self.settings.beginWriteArray("usernames") self.usernames.add(self.newUsername) for i, username in enumerate(self.usernames): self.settings.setArrayIndex(i) self.settings.setValue("usernames", username) self.settings.endArray() self.settings.beginWriteArray("engines") self.engines.add(self.newEnginePath) for i, path in enumerate(self.engines): self.settings.setArrayIndex(i) self.settings.setValue("engines", path) self.settings.endArray() self.accept()
class SettingsWidget(QWidget): """Widget to show and edit the settings.""" def __init__(self): super(SettingsWidget, self).__init__() self.settings = QSettings(c.SETTINGS_PATH, QSettings.IniFormat) self.keyboard_settings = QSettings(c.KEYBOARD_SETTINGS_PATH, QSettings.IniFormat) self.value_dict_settings = {} self.value_dict_keyboard = {} self.f_reg = re.compile("F[1-9]|1[0-2]") self.ctrl_reg = re.compile("CTRL\+[a-xA-X0-9]") self.forbidden_reg = re.compile("CTRL\+[vVcCzZyY]") v_box = QVBoxLayout() theme = QGroupBox("Theme") theme_layout = QFormLayout() theme_value = QComboBox() theme_value.addItem(c.THEME_D) theme_value.addItem(c.THEME_L) theme_value.setCurrentText( self.settings.value(c.THEME, defaultValue=c.THEME_D)) theme_layout.addRow(QLabel(c.THEME.capitalize()), theme_value) theme.setLayout(theme_layout) self.value_dict_settings[c.THEME] = theme_value keyboard = QGroupBox("Keyboard") keyboard_layout = QFormLayout() info_label = QLabel("Only F-Keys and CTRL+ Combinations are allowed.") info_label.setWordWrap(True) keyboard_layout.addRow(info_label) for key in self.keyboard_settings.allKeys(): line_edit = QLineEdit() line_edit.setText(self.keyboard_settings.value(key)) keyboard_layout.addRow(key, line_edit) self.value_dict_keyboard[key] = line_edit keyboard.setLayout(keyboard_layout) plugins = QGroupBox("Plug-ins") plugins_layout = QFormLayout() show_empty_buttons_check = QCheckBox( "Show Plug-ins without Buttons in Word By Word - List") show_empty_buttons_check.setChecked( self.settings.value(c.SHOW_EMPTY_BUTTONS, defaultValue=True, type=bool)) plugins_layout.addRow(show_empty_buttons_check) self.value_dict_settings[ c.SHOW_EMPTY_BUTTONS] = show_empty_buttons_check plugins.setLayout(plugins_layout) self.note = QLabel("Settings will be applied after restart") save = QPushButton("Save") save.clicked.connect(self.save_values) v_box.addWidget(theme) v_box.addWidget(keyboard) v_box.addWidget(plugins) v_box.addWidget(self.note) v_box.addWidget(save) self.setLayout(v_box) def save_values(self): """Saves the current values.""" saved = True for key in self.value_dict_settings.keys(): widget = self.value_dict_settings.get(key) if isinstance(widget, QLineEdit): self.settings.setValue(key, widget.text().strip()) if isinstance(widget, QComboBox): self.settings.setValue(key, widget.currentText().strip()) if isinstance(widget, QCheckBox): self.settings.setValue(key, widget.isChecked()) occupied = [] self.keyboard_settings.clear() for key in self.value_dict_keyboard.keys(): value = self.value_dict_keyboard.get(key).text().strip().replace( " ", "").upper() if len(value) == 0 or ((bool(self.f_reg.search(value)) or bool(self.ctrl_reg.search(value))) and not bool(self.forbidden_reg.search(value))): if value not in occupied: self.keyboard_settings.setValue(key, value) if len(value) != 0: occupied.append(value) else: saved = False self.note.setStyleSheet("QLabel {color: red;}") self.note.setText("Duplicate Values found!") self.keyboard_settings.setValue(key, "") if saved: self.note.setStyleSheet("QLabel {color: green;}") self.note.setText("Saved!") def reset_note(self): """Resets the little now text at the bottom.""" self.note.setStyleSheet("") self.note.setText("Settings will be applied after restart")