def set_format(self, column): str_format = Qt.ISODate if self.have_date and self.have_time: self.setDisplayFormat("yyyy-MM-dd hh:mm:ss") c_format = "%Y-%m-%d %H:%M:%S" min_datetime, max_datetime = self.find_range(column, c_format) self.min_datetime = QDateTime.fromString(min_datetime, str_format) self.max_datetime = QDateTime.fromString(max_datetime, str_format) self.setCalendarPopup(True) self.calendarWidget = gui.CalendarWidgetWithTime( self, time=self.min_datetime.time()) self.calendarWidget.timeedit.timeChanged.connect(self.set_datetime) self.setCalendarWidget(self.calendarWidget) self.setDateTimeRange(self.min_datetime, self.max_datetime) elif self.have_date and not self.have_time: self.setDisplayFormat("yyyy-MM-dd") self.setCalendarPopup(True) min_datetime, max_datetime = self.find_range(column, "%Y-%m-%d") self.min_datetime = QDate.fromString(min_datetime, str_format) self.max_datetime = QDate.fromString(max_datetime, str_format) self.setDateRange(self.min_datetime, self.max_datetime) elif not self.have_date and self.have_time: self.setDisplayFormat("hh:mm:ss") min_datetime, max_datetime = self.find_range(column, "%H:%M:%S") self.min_datetime = QTime.fromString(min_datetime, str_format) self.max_datetime = QTime.fromString(max_datetime, str_format) self.setTimeRange(self.min_datetime, self.max_datetime)
def valuesChanged(self, minValue, maxValue): self.slider_values = (minValue, maxValue) self._delta = max(1, (maxValue - minValue)) minTime = self.slider.scale(minValue) maxTime = self.slider.scale(maxValue) from_dt = QDateTime.fromMSecsSinceEpoch(minTime * 1000).toUTC() to_dt = QDateTime.fromMSecsSinceEpoch(maxTime * 1000).toUTC() with blockSignals(self.date_from, self.date_to): self.date_from.setDateTime(from_dt) self.date_to.setDateTime(to_dt) self.send_selection(minTime, maxTime)
def test_no_date_no_time(self): callback = Mock() editor = TimeVariableEditor(self.parent, TimeVariable("var"), callback) self.assertEqual(editor.value, 0) self.assertEqual(self.editor._edit.dateTime(), QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0))) self.callback.assert_not_called() datetime = QDateTime(QDate(2001, 9, 9), QTime(1, 2, 3)) editor._edit.setDateTime(datetime) self.assertEqual(editor._edit.dateTime(), datetime) self.assertEqual(editor.value, 999993600 + 3723) callback.assert_called_once()
def valuesChanged(self, minValue, maxValue): self.slider_values = (minValue, maxValue) self._delta = max(1, (maxValue - minValue)) minTime = self.slider.scale(minValue) maxTime = self.slider.scale(maxValue) from_dt = QDateTime.fromMSecsSinceEpoch(minTime * 1000).toUTC() to_dt = QDateTime.fromMSecsSinceEpoch(maxTime * 1000).toUTC() with blockSignals(self.date_from, self.date_to): self.date_from.setDateTime(from_dt) self.date_to.setDateTime(to_dt) self.send_selection(minTime, maxTime)
def test_edit(self): """ Edit datetimeedit by user. """ datetime = QDateTime(QDate(2001, 9, 9)) self.editor._edit.setDateTime(datetime) self.assertEqual(self.editor.value, 999993600) self.assertEqual(self.editor._edit.dateTime(), datetime) self.callback.assert_called_once()
def test_set_value(self): """ Programmatically set datetimeedit value. """ value = 999993600 self.editor.value = value self.assertEqual(self.editor._edit.dateTime(), QDateTime(QDate(2001, 9, 9))) self.assertEqual(self.editor.value, value) self.callback.assert_called_once()
def test_set_datetime(self): c = gui.DateTimeEditWCalendarTime(None) # default time (now) c.set_datetime() self.assertLessEqual( abs(c.dateTime().toSecsSinceEpoch() - time.time()), 2) # some date poeh = QDateTime(QDate(1961, 4, 12), QTime(6, 7)) c.set_datetime(poeh) self.assertEqual(c.dateTime(), poeh) # set a different time ali = QTime(8, 5) c.set_datetime(ali) poeh.setTime(ali) self.assertEqual(c.dateTime(), poeh)
def set_data(self, data): slider = self.slider self.data = data = None if data is None else Timeseries.from_data_table( data) def disabled(): slider.setFormatter(str) slider.setHistogram(None) slider.setScale(0, 0) slider.setValues(0, 0) slider.setDisabled(True) self.send('Subset', None) if data is None: disabled() return if not isinstance(data.time_variable, TimeVariable): self.Error.no_time_variable() disabled() return self.Error.clear() var = data.time_variable time_values = data.time_values slider.setDisabled(False) slider.setHistogram(time_values) slider.setFormatter(var.repr_val) slider.setScale(time_values.min(), time_values.max()) self.valuesChanged(slider.minimumValue(), slider.maximumValue()) # Update datetime edit fields min_dt = QDateTime.fromMSecsSinceEpoch(time_values[0] * 1000).toUTC() max_dt = QDateTime.fromMSecsSinceEpoch(time_values[-1] * 1000).toUTC() self.date_from.setDateTimeRange(min_dt, max_dt) self.date_to.setDateTimeRange(min_dt, max_dt) date_format = ' '.join( (self.DATE_FORMATS[0] if var.have_date else '', self.DATE_FORMATS[1] if var.have_time else '')).strip() self.date_from.setDisplayFormat(date_format) self.date_to.setDisplayFormat(date_format)
def node_info_complete(self, future): assert self.thread() is QThread.currentThread() assert future.done() self._task = None self.progressBarFinished() self.setCursor(Qt.ArrowCursor) self.btn_connect.setText(self.LABEL_CONNECT) self.Information.fetching_node_info.clear() try: lst, min_time, max_time = future.result( ) # type: Tuple[List[List], pd.Timestamp, pd.Timestamp] except self.Cancelled: pass except Exception as e: log.exception("Error fetching node info") self.Error.fetching_node_info_failed(e) else: self.model.wrap(lst) CachedNodeInfoTable.dump_list(lst) self.btn_download.setEnabled(True) # Apply saved row selection if self.selection: try: selection = QItemSelection() for row in self.model.mapFromSourceRows(self.selection): selection.select(self.model.index(row, 0)) self.view.selectionModel().select(selection) except Exception: log.exception('Failed to restore selection') self.selection = [] self.date_from.setDateTime( QDateTime.fromMSecsSinceEpoch(min_time.timestamp() * 1000, Qt.UTC)) self.date_to.setDateTime( QDateTime.fromMSecsSinceEpoch(max_time.timestamp() * 1000, Qt.UTC))
def set_data(self, data): slider = self.slider self.data = data = None if data is None else Timeseries.from_data_table(data) def disabled(): slider.setFormatter(str) slider.setHistogram(None) slider.setScale(0, 0) slider.setValues(0, 0) self._set_disabled(True) self.Outputs.subset.send(None) if data is None: disabled() return if not isinstance(data.time_variable, TimeVariable): self.Error.no_time_variable() disabled() return self.Error.clear() var = data.time_variable time_values = data.time_values self._set_disabled(False) slider.setHistogram(time_values) slider.setFormatter(var.repr_val) slider.setScale(time_values.min(), time_values.max()) self.valuesChanged(slider.minimumValue(), slider.maximumValue()) # Update datetime edit fields min_dt = QDateTime.fromMSecsSinceEpoch(time_values[0] * 1000).toUTC() max_dt = QDateTime.fromMSecsSinceEpoch(time_values[-1] * 1000).toUTC() self.date_from.setDateTimeRange(min_dt, max_dt) self.date_to.setDateTimeRange(min_dt, max_dt) date_format = ' '.join((self.DATE_FORMATS[0] if var.have_date else '', self.DATE_FORMATS[1] if var.have_time else '')).strip() self.date_from.setDisplayFormat(date_format) self.date_to.setDisplayFormat(date_format)
def test_have_time(self): callback = Mock() editor = TimeVariableEditor(self.parent, TimeVariable("var", have_time=1), callback) self.assertEqual(editor.value, 0) self.assertEqual(self.editor._edit.dateTime(), _datetime(1970, 1, 1)) self.callback.assert_not_called() datetime = QDateTime(QDate(1900, 1, 1), QTime(1, 2, 3)) editor._edit.setDateTime(datetime) self.assertEqual(editor._edit.dateTime(), datetime) self.assertEqual(editor.value, 3723) callback.assert_called_once()
def set_datetime(self, date_time): if not date_time: date_time = self.min_datetime if self.have_date and self.have_time: if isinstance(date_time, QTime): self.setDateTime( QDateTime(self.date(), self.calendarWidget.timeedit.time())) else: self.setDateTime(date_time) elif self.have_date and not self.have_time: self.setDate(date_time) elif not self.have_date and self.have_time: self.setTime(date_time)
def dteditValuesChanged(self, minTime, maxTime): minValue = self.slider.unscale(minTime) maxValue = self.slider.unscale(maxTime) if minValue == maxValue: # maxValue's range is minValue's range shifted by one maxValue += 1 maxTime = self.slider.scale(maxValue) to_dt = QDateTime.fromMSecsSinceEpoch(maxTime * 1000).toUTC() with blockSignals(self.date_to): self.date_to.setDateTime(to_dt) self._delta = max(1, (maxValue - minValue)) if self.slider_values != (minValue, maxValue): self.slider_values = (minValue, maxValue) with blockSignals(self.slider): self.slider.setValues(minValue, maxValue) self.send_selection(minTime, maxTime)
def set_data(self, data): self.cancel() self.closeContext() self.varmodel[:] = [] self._variable_imputation_state = {} # type: VariableState self.modified = False self.data = data if data is not None: self.varmodel[:] = data.domain.variables self.openContext(data.domain) self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(self.default_time)) # restore per variable imputation state self._restore_state(self._variable_imputation_state) summary = len(data) if data else self.info.NoInput details = format_summary_details(data) if data else "" self.info.set_input_summary(summary, details) self.update_varview() self.unconditional_commit()
def __map_to_datetime(self, value: float) -> QDateTime: return QDateTime.fromString(self._variable.repr_val(value), self._format)
def __map_from_datetime(self, date_time: QDateTime) -> float: return self._variable.to_val(date_time.toString(self._format))
def __init__(self): super().__init__() self.data = None # type: Optional[Orange.data.Table] self.learner = None # type: Optional[Learner] self.default_learner = SimpleTreeLearner(min_instances=10, max_depth=10) self.modified = False self.executor = qconcurrent.ThreadExecutor(self) self.__task = None main_layout = self.controlArea.layout() box = gui.vBox(self.controlArea, "默认方法") box_layout = QGridLayout() box_layout.setSpacing(8) box.layout().addLayout(box_layout) button_group = QButtonGroup() button_group.buttonClicked[int].connect(self.set_default_method) for i, (method, _) in enumerate(list(METHODS.items())[1:-1]): imputer = self.create_imputer(method) button = QRadioButton(imputer.name) button.setChecked(method == self.default_method_index) button_group.addButton(button, method) box_layout.addWidget(button, i % 3, i // 3) def set_default_time(datetime): datetime = datetime.toSecsSinceEpoch() if datetime != self.default_time: self.default_time = datetime if self.default_method_index == Method.Default: self._invalidate() hlayout = QHBoxLayout() box.layout().addLayout(hlayout) button = QRadioButton("固定值; 数值变量:") button_group.addButton(button, Method.Default) button.setChecked(Method.Default == self.default_method_index) hlayout.addWidget(button) self.numeric_value_widget = DoubleSpinBox( minimum=DBL_MIN, maximum=DBL_MAX, singleStep=.1, value=self.default_numeric_value, alignment=Qt.AlignRight, enabled=self.default_method_index == Method.Default, ) self.numeric_value_widget.editingFinished.connect( self.__on_default_numeric_value_edited) self.connect_control("default_numeric_value", self.numeric_value_widget.setValue) hlayout.addWidget(self.numeric_value_widget) hlayout.addWidget(QLabel(", 时间:")) self.time_widget = gui.DateTimeEditWCalendarTime(self) self.time_widget.setEnabled( self.default_method_index == Method.Default) self.time_widget.setKeyboardTracking(False) self.time_widget.setContentsMargins(0, 0, 0, 0) self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(self.default_time)) self.connect_control( "default_time", lambda value: self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(value))) self.time_widget.dateTimeChanged.connect(set_default_time) hlayout.addWidget(self.time_widget) self.default_button_group = button_group box = gui.hBox(self.controlArea, self.tr("设置单个属性"), flat=False) self.varview = ListViewSearch( selectionMode=QListView.ExtendedSelection, uniformItemSizes=True) self.varview.setItemDelegate(DisplayFormatDelegate()) self.varmodel = itemmodels.VariableListModel() self.varview.setModel(self.varmodel) self.varview.selectionModel().selectionChanged.connect( self._on_var_selection_changed) self.selection = self.varview.selectionModel() box.layout().addWidget(self.varview) vertical_layout = QVBoxLayout(margin=0) self.methods_container = QWidget(enabled=False) method_layout = QVBoxLayout(margin=0) self.methods_container.setLayout(method_layout) button_group = QButtonGroup() for method in Method: imputer = self.create_imputer(method) button = QRadioButton(text=imputer.name) button_group.addButton(button, method) method_layout.addWidget(button) self.value_combo = QComboBox( minimumContentsLength=8, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLengthWithIcon, activated=self._on_value_selected) self.value_double = DoubleSpinBox( editingFinished=self._on_value_selected, minimum=DBL_MIN, maximum=DBL_MAX, singleStep=.1, ) self.value_stack = value_stack = QStackedWidget() value_stack.addWidget(self.value_combo) value_stack.addWidget(self.value_double) method_layout.addWidget(value_stack) button_group.buttonClicked[int].connect( self.set_method_for_current_selection) self.reset_button = QPushButton( "Restore All to Default", enabled=False, default=False, autoDefault=False, clicked=self.reset_variable_state, ) vertical_layout.addWidget(self.methods_container) vertical_layout.addStretch(2) vertical_layout.addWidget(self.reset_button) box.layout().addLayout(vertical_layout) self.variable_button_group = button_group gui.auto_apply(self.buttonsArea, self, "autocommit")
def __init__(self): super().__init__() self.data = None # type: Optional[Orange.data.Table] self.learner = None # type: Optional[Learner] self.default_learner = SimpleTreeLearner(min_instances=10, max_depth=10) self.modified = False self.executor = qconcurrent.ThreadExecutor(self) self.__task = None main_layout = QVBoxLayout() main_layout.setContentsMargins(10, 10, 10, 10) self.controlArea.layout().addLayout(main_layout) box = gui.vBox(None, "Default Method") main_layout.addWidget(box) box_layout = QGridLayout() box_layout.setSpacing(8) box.layout().addLayout(box_layout) button_group = QButtonGroup() button_group.buttonClicked[int].connect(self.set_default_method) for i, (method, _) in enumerate(list(METHODS.items())[1:-1]): imputer = self.create_imputer(method) button = QRadioButton(imputer.name) button.setChecked(method == self.default_method_index) button_group.addButton(button, method) box_layout.addWidget(button, i % 3, i // 3) def set_default_time(datetime): datetime = datetime.toSecsSinceEpoch() if datetime != self.default_time: self.default_time = datetime if self.default_method_index == Method.Default: self._invalidate() hlayout = QHBoxLayout() box.layout().addLayout(hlayout) button = QRadioButton("Fixed values; numeric variables:") button_group.addButton(button, Method.Default) button.setChecked(Method.Default == self.default_method_index) hlayout.addWidget(button) locale = QLocale() locale.setNumberOptions(locale.NumberOption.RejectGroupSeparator) validator = QDoubleValidator() validator.setLocale(locale) self.numeric_value_widget = le = gui.lineEdit( None, self, "default_numeric", validator=validator, alignment=Qt.AlignRight, callback=self._invalidate, enabled=self.default_method_index == Method.Default) hlayout.addWidget(le) hlayout.addWidget(QLabel(", time:")) self.time_widget = gui.DateTimeEditWCalendarTime(self) self.time_widget.setEnabled( self.default_method_index == Method.Default) self.time_widget.setKeyboardTracking(False) self.time_widget.setContentsMargins(0, 0, 0, 0) self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(self.default_time)) self.connect_control( "default_time", lambda value: self.time_widget.set_datetime( QDateTime.fromSecsSinceEpoch(value))) self.time_widget.dateTimeChanged.connect(set_default_time) hlayout.addWidget(self.time_widget) self.default_button_group = button_group box = QGroupBox(title=self.tr("Individual Attribute Settings"), flat=False) main_layout.addWidget(box) horizontal_layout = QHBoxLayout(box) main_layout.addWidget(box) self.varview = ListViewSearch( selectionMode=QListView.ExtendedSelection, uniformItemSizes=True) self.varview.setItemDelegate(DisplayFormatDelegate()) self.varmodel = itemmodels.VariableListModel() self.varview.setModel(self.varmodel) self.varview.selectionModel().selectionChanged.connect( self._on_var_selection_changed) self.selection = self.varview.selectionModel() horizontal_layout.addWidget(self.varview) vertical_layout = QVBoxLayout(margin=0) self.methods_container = QWidget(enabled=False) method_layout = QVBoxLayout(margin=0) self.methods_container.setLayout(method_layout) button_group = QButtonGroup() for method in Method: imputer = self.create_imputer(method) button = QRadioButton(text=imputer.name) button_group.addButton(button, method) method_layout.addWidget(button) self.value_combo = QComboBox( minimumContentsLength=8, sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLength, activated=self._on_value_selected) self.value_double = QDoubleSpinBox( editingFinished=self._on_value_selected, minimum=-1000., maximum=1000., singleStep=.1, decimals=3, ) self.value_stack = value_stack = QStackedWidget() value_stack.addWidget(self.value_combo) value_stack.addWidget(self.value_double) method_layout.addWidget(value_stack) button_group.buttonClicked[int].connect( self.set_method_for_current_selection) self.reset_button = QPushButton( "Restore All to Default", enabled=False, default=False, autoDefault=False, clicked=self.reset_variable_state, ) vertical_layout.addWidget(self.methods_container) vertical_layout.addStretch(2) vertical_layout.addWidget(self.reset_button) horizontal_layout.addLayout(vertical_layout) self.variable_button_group = button_group box = gui.auto_apply(self.controlArea, self, "autocommit") box.button.setFixedWidth(180) box.layout().insertStretch(0) self.info.set_input_summary(self.info.NoInput) self.info.set_output_summary(self.info.NoOutput)
def test_init(self): self.assertEqual(self.editor.value, 0) self.assertEqual(self.editor._edit.dateTime(), QDateTime(QDate(1970, 1, 1))) self.callback.assert_not_called()
def format_time(i): dt = QDateTime.fromMSecsSinceEpoch(i * 1000).toUTC() return dt.toString(date_format)
def _datetime(y, m, d) -> QDateTime: return QDateTime(QDate(y, m, d), QTime(0, 0))
def utc_dt(dt): qdt = QDateTime(dt) qdt.setTimeZone(QTimeZone.utc()) return qdt
def __init__(self): self.model = None self._task = None # type: Optional[self.Task] self._executor = ThreadExecutor(self) self.is_downloading = False box = gui.vBox(self.controlArea, 'Database Connection') gui.lineEdit(box, self, 'con_hostname', label='Hostname:', orientation=Qt.Horizontal, validator=Validator.Hostname()) gui.lineEdit(box, self, 'con_port', label='Port:', orientation=Qt.Horizontal, validator=Validator.Port()) gui.lineEdit(box, self, 'con_username', label='Username:'******'con_password', label='Password:'******'con_database', label='Database:', orientation=Qt.Horizontal) gui.spin(box, self, 'con_timeout', 5, 300, 5, label='Timeout [s]:') self.btn_connect = gui.button(box, self, self.LABEL_CONNECT, callback=self.load_data) box = gui.vBox(self.controlArea, 'Download') def _dateTimeChanged(editted): def handler(): minTime = self.date_from.dateTime().toMSecsSinceEpoch() / 1000 maxTime = self.date_to.dateTime().toMSecsSinceEpoch() / 1000 if minTime > maxTime: minTime = maxTime = minTime if editted == self.date_from else maxTime other = self.date_to if editted == self.date_from else self.date_from with blockSignals(other): other.setDateTime(editted.dateTime()) self.btn_download.setEnabled(minTime != maxTime) # Update saved settings self.sample_ts_from = minTime self.sample_ts_to = maxTime return handler kwargs = dict(calendarPopup=True, displayFormat=' '.join(self.DATE_FORMATS), timeSpec=Qt.UTC) date_from = self.date_from = QDateTimeEdit(self, **kwargs) date_to = self.date_to = QDateTimeEdit(self, **kwargs) date_from.setDateTime( QDateTime.fromMSecsSinceEpoch(self.sample_ts_from * 1000, Qt.UTC)) date_to.setDateTime( QDateTime.fromMSecsSinceEpoch(self.sample_ts_to * 1000, Qt.UTC)) date_from.dateTimeChanged.connect(_dateTimeChanged(date_from)) date_to.dateTimeChanged.connect(_dateTimeChanged(date_to)) hbox = gui.hBox(box) hbox.layout().addWidget(QLabel('From:')) hbox.layout().addWidget(date_from) hbox = gui.hBox(box) hbox.layout().addWidget(QLabel('To:')) hbox.layout().addWidget(date_to) self.box_include_data = gui.vBox(box, 'Include') gui.spin(box, self, 'sample_size', 100, 20000, 100, label='Sample size:') gui.comboBox(box, self, 'sample_resolution', label='Resolution:', orientation=Qt.Horizontal, items=tuple(self.RESOLUTION.keys()), sendSelectedValue=True) gui.comboBox(box, self, 'sample_interpolation', label='Interpolation:', orientation=Qt.Horizontal, items=tuple(self.INTERPOLATION.keys()), sendSelectedValue=True) self.btn_download = gui.button(box, self, self.LABEL_DOWNLOAD, callback=self.download) gui.rubber(self.controlArea) ## Main area class Model(PyTableModel): def update_row(self, i, row): self[i] = row model = self.model = Model(parent=self) model.setHorizontalHeaderLabels([ 'Node Id', 'Interfaces', 'Start Time', 'Stop Time', 'Available Data' ]) view = self.view = gui.TableView(self) view.horizontalHeader().setStretchLastSection(False) view.setModel(self.model) self.mainArea.layout().addWidget(view) # Restore node info table from cache, if any try: lst = CachedNodeInfoTable.load_list() except Exception: pass # Cache not exists else: model.wrap(lst) # Restore tables checkboxes from cache try: tables = CachedNodeInfoTable.load_tables() except Exception: pass else: for table in tables: self.box_include_data.layout().addWidget( QCheckBox(table, self, checked=table in self.included_data)) # Establish default database connection from settings set_connection_params(self.con_hostname, self.con_port, self.con_username, self.con_password, self.con_database, timeout=self.con_timeout)