Skip to content

Commit

Permalink
Merge pull request #358 from altlinux/add-creation-buttons-to-toolbar
Browse files Browse the repository at this point in the history
Add creation buttons to toolbar
  • Loading branch information
Kvel2D committed Apr 1, 2022
2 parents c2266b6 + 5f13caa commit 412b378
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 12 deletions.
89 changes: 77 additions & 12 deletions src/admc/console_impls/object_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ QList<QString> index_list_to_dn_list(const QList<QModelIndex> &index_list);
QList<QString> get_selected_dn_list_object(ConsoleWidget *console);
QString get_selected_target_dn_object(ConsoleWidget *console);
void console_object_delete(ConsoleWidget *console, const QList<QString> &dn_list, const QModelIndex &tree_root);
bool can_create_class_at_parent(const QString &create_class, const QString &parent_class);

ObjectImpl::ObjectImpl(ConsoleWidget *console_arg)
: ConsoleImpl(console_arg) {
Expand All @@ -80,6 +81,10 @@ ObjectImpl::ObjectImpl(ConsoleWidget *console_arg)
find_action_enabled = true;
refresh_action_enabled = true;

toolbar_create_user = nullptr;
toolbar_create_group = nullptr;
toolbar_create_ou = nullptr;

object_filter = settings_get_variant(SETTING_object_filter).toString();
object_filter_enabled = settings_get_variant(SETTING_object_filter_enabled).toBool();

Expand Down Expand Up @@ -158,6 +163,9 @@ ObjectImpl::ObjectImpl(ConsoleWidget *console_arg)
connect(
edit_upn_suffixes_action, &QAction::triggered,
this, &ObjectImpl::on_edit_upn_suffixes);
connect(
console, &ConsoleWidget::selection_changed,
this, &ObjectImpl::update_toolbar_actions);
}

void ObjectImpl::set_policy_impl(PolicyImpl *policy_impl_arg) {
Expand Down Expand Up @@ -362,18 +370,7 @@ QSet<QAction *> ObjectImpl::get_custom_actions(const QModelIndex &index, const b
for (const QString &action_object_class : new_action_map.keys()) {
QAction *action = new_action_map[action_object_class];

const bool is_visible = [&action_object_class, &object_class]() {
// NOTE: to get full list of possible
// superiors, need to use the all of the parent
// classes too, not just the leaf class
const QList<QString> action_object_class_list = g_adconfig->get_inherit_chain(action_object_class);
const QList<QString> possible_superiors = g_adconfig->get_possible_superiors(QList<QString>(action_object_class_list));
const bool is_visible_out = possible_superiors.contains(object_class);

return is_visible_out;
}();


const bool is_visible = can_create_class_at_parent(action_object_class, object_class);
action->setVisible(is_visible);
}

Expand Down Expand Up @@ -644,6 +641,22 @@ void ObjectImpl::set_refresh_action_enabled(const bool enabled) {
refresh_action_enabled = enabled;
}

void ObjectImpl::set_toolbar_actions(QAction *toolbar_create_user_arg, QAction *toolbar_create_group_arg, QAction *toolbar_create_ou_arg) {
toolbar_create_user = toolbar_create_user_arg;
toolbar_create_group = toolbar_create_group_arg;
toolbar_create_ou = toolbar_create_ou_arg;

connect(
toolbar_create_user, &QAction::triggered,
this, &ObjectImpl::on_new_user);
connect(
toolbar_create_group, &QAction::triggered,
this, &ObjectImpl::on_new_group);
connect(
toolbar_create_ou, &QAction::triggered,
this, &ObjectImpl::on_new_ou);
}

QList<QString> ObjectImpl::column_labels() const {
return object_impl_column_labels();
}
Expand Down Expand Up @@ -1284,6 +1297,47 @@ void ObjectImpl::move(AdInterface &ad, const QList<QString> &old_dn_list, const
move_and_rename(ad, old_to_new_dn_map, new_parent_dn);
}

void ObjectImpl::update_toolbar_actions() {
const QHash<QString, QAction *> toolbar_action_map = {
{CLASS_USER, toolbar_create_user},
{CLASS_GROUP, toolbar_create_group},
{CLASS_OU, toolbar_create_ou},
};

// Disable all actions by default
for (const QString &action_object_class : toolbar_action_map.keys()) {
QAction *action = toolbar_action_map[action_object_class];

if (action == nullptr) {
continue;
}

action->setEnabled(false);
}

// Then enable them depending on current selection
const QList<QModelIndex> target_list = console->get_selected_items(ItemType_Object);

const bool single_selection = (target_list.size() == 1);
if (!single_selection) {
return;
}

const QModelIndex target = target_list[0];
const QString object_class = target.data(ObjectRole_ObjectClasses).toStringList().last();

for (const QString &action_object_class : toolbar_action_map.keys()) {
QAction *action = toolbar_action_map[action_object_class];

if (action == nullptr) {
continue;
}

const bool is_enabled = can_create_class_at_parent(action_object_class, object_class);
action->setEnabled(is_enabled);
}
}

void object_impl_add_objects_to_console(ConsoleWidget *console, const QList<AdObject> &object_list, const QModelIndex &parent) {
if (!parent.isValid()) {
return;
Expand Down Expand Up @@ -1694,3 +1748,14 @@ void console_object_delete(ConsoleWidget *console, const QList<QString> &dn_list
}
}
}

bool can_create_class_at_parent(const QString &create_class, const QString &parent_class) {
// NOTE: to get full list of possible
// superiors, need to use the all of the parent
// classes too, not just the leaf class
const QList<QString> action_object_class_list = g_adconfig->get_inherit_chain(create_class);
const QList<QString> possible_superiors = g_adconfig->get_possible_superiors(QList<QString>(action_object_class_list));
const bool out = possible_superiors.contains(parent_class);

return out;
}
6 changes: 6 additions & 0 deletions src/admc/console_impls/object_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ObjectImpl final : public ConsoleImpl {

void set_find_action_enabled(const bool enabled);
void set_refresh_action_enabled(const bool enabled);
void set_toolbar_actions(QAction *create_user, QAction *create_group, QAction *create_ou);

QList<QString> column_labels() const override;
QList<int> default_columns() const override;
Expand Down Expand Up @@ -131,6 +132,10 @@ private slots:
QAction *new_action;
QHash<QString, QAction *> new_action_map;

QAction *toolbar_create_user;
QAction *toolbar_create_group;
QAction *toolbar_create_ou;

bool find_action_enabled;
bool refresh_action_enabled;

Expand All @@ -140,6 +145,7 @@ private slots:
void drop_policies(const QList<QPersistentModelIndex> &dropped_list, const QPersistentModelIndex &target);
void move_and_rename(AdInterface &ad, const QHash<QString, QString> &old_dn_list, const QString &new_parent_dn);
void move(AdInterface &ad, const QList<QString> &old_dn_list, const QString &new_parent_dn);
void update_toolbar_actions();
};

void object_impl_add_objects_to_console(ConsoleWidget *console, const QList<AdObject> &object_list, const QModelIndex &parent);
Expand Down
14 changes: 14 additions & 0 deletions src/admc/console_widget/console_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ ConsoleWidget::ConsoleWidget(QWidget *parent)
connect(
d->scope_view->selectionModel(), &QItemSelectionModel::currentChanged,
d, &ConsoleWidgetPrivate::on_current_scope_item_changed);
connect(
d->scope_view->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &ConsoleWidget::selection_changed);
connect(
d->model, &QStandardItemModel::rowsAboutToBeRemoved,
d, &ConsoleWidgetPrivate::on_scope_items_about_to_be_removed);
Expand Down Expand Up @@ -205,6 +208,9 @@ void ConsoleWidget::register_impl(const int type, ConsoleImpl *impl) {
connect(
results_view, &ResultsView::context_menu,
d, &ConsoleWidgetPrivate::on_results_context_menu);
connect(
results_view, &ResultsView::selection_changed,
this, &ConsoleWidget::selection_changed);
}
}

Expand Down Expand Up @@ -784,6 +790,12 @@ void ConsoleWidgetPrivate::set_results_to_type(const ResultsViewType type) {

if (impl->view() != nullptr) {
impl->view()->set_view_type(type);

// NOTE: changing results type causes a
// selection change because selection is there
// is a separate selection for each type of
// results view
emit q->selection_changed();
}
}

Expand Down Expand Up @@ -1019,6 +1031,8 @@ void ConsoleWidgetPrivate::on_focus_changed(QWidget *old, QWidget *now) {

if (new_focused_view == scope_view || new_focused_view == results_view) {
focused_view = new_focused_view;

emit q->selection_changed();
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/admc/console_widget/console_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class ConsoleWidget final : public QWidget {
// opened by right click is setup automatically.
void setup_menubar_action_menu(QMenu *menu);

signals:
// Emitted when selection in the whole console
// widget changes, both in scope and results panes.
// Can be caused by selection change in focused
// view or change of which view is focused.
void selection_changed();

private:
ConsoleWidgetPrivate *d;

Expand Down
4 changes: 4 additions & 0 deletions src/admc/console_widget/results_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ ResultsView::ResultsView(QWidget *parent)
// are connected directly, deletion of results
// models becomes extremely slow.
view->setModel(proxy_model);

connect(
view->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &ResultsView::selection_changed);
}

set_drag_drop_enabled(true);
Expand Down
1 change: 1 addition & 0 deletions src/admc/console_widget/results_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ResultsView final : public QWidget {
signals:
void activated(const QModelIndex &index);
void context_menu(const QPoint pos);
void selection_changed();

private:
QStackedWidget *stacked_widget;
Expand Down
2 changes: 2 additions & 0 deletions src/admc/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ MainWindow::MainWindow(AdInterface &ad, QWidget *parent)
object_impl->set_policy_impl(policy_impl);
query_item_impl->set_query_folder_impl(query_folder_impl);

object_impl->set_toolbar_actions(ui->action_create_user, ui->action_create_group, ui->action_create_ou);

// Setup console
const ConsoleWidgetActions console_actions = [&]() {
ConsoleWidgetActions out;
Expand Down
49 changes: 49 additions & 0 deletions src/admc/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
<addaction name="separator"/>
<addaction name="action_refresh"/>
<addaction name="action_manual"/>
<addaction name="separator"/>
<addaction name="action_create_user"/>
<addaction name="action_create_group"/>
<addaction name="action_create_ou"/>
</widget>
<widget class="QDockWidget" name="message_log">
<property name="windowTitle">
Expand Down Expand Up @@ -342,6 +346,51 @@
<string>&amp;Operations Masters</string>
</property>
</action>
<action name="action_create_user">
<property name="icon">
<iconset theme="avatar-default-symbolic">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Create user</string>
</property>
<property name="toolTip">
<string>Create user (Alt + 7)</string>
</property>
<property name="shortcut">
<string>Alt+7</string>
</property>
</action>
<action name="action_create_group">
<property name="icon">
<iconset theme="system-users">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Create group</string>
</property>
<property name="toolTip">
<string>Create group (Alt + 6)</string>
</property>
<property name="shortcut">
<string>Alt+6</string>
</property>
</action>
<action name="action_create_ou">
<property name="icon">
<iconset theme="folder-documents">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>Create organization unit</string>
</property>
<property name="toolTip">
<string>Create organization unit (Alt + 5)</string>
</property>
<property name="shortcut">
<string>Alt+5</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down

0 comments on commit 412b378

Please sign in to comment.