summaryrefslogtreecommitdiff
path: root/indra/newview/llsettingspicker.cpp
blob: 85f0678c4c555a20e5d607f4a23e26b9cb7848b1 (plain)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/**
* @author Rider Linden
* @brief LLSettingsPicker class header file including related functions
*
* $LicenseInfo:firstyear=2018&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2018, Linden Research, Inc.
*
* This library 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;
* version 2.1 of the License only.
*
* This library 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 library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

#include "llviewerprecompiledheaders.h"

#include "llsettingspicker.h"

#include "llcombobox.h"
#include "llfiltereditor.h"
#include "llfolderviewmodel.h"
#include "llinventory.h"
#include "llinventorybridge.h"
#include "llinventoryfunctions.h"
#include "llinventorymodelbackgroundfetch.h"
#include "llinventoryobserver.h"
#include "llinventorypanel.h"
#include "llsettingsvo.h"

#include "lldraghandle.h"
#include "llviewercontrol.h"
#include "llagent.h"

//=========================================================================
namespace
{
    const std::string FLOATER_DEFINITION_XML("floater_settings_picker.xml");

    const std::string FLT_INVENTORY_SEARCH("flt_inventory_search");
    const std::string CMB_TRACK_SELECTION("track_selection");
    const std::string PNL_INVENTORY("pnl_inventory");
    const std::string PNL_COMBO("pnl_combo");
    const std::string BTN_SELECT("btn_select");
    const std::string BTN_CANCEL("btn_cancel");

    // strings in xml

    const std::string STR_TITLE_PREFIX = "pick title";
    const std::string STR_TITLE_TRACK = "pick_track";
    const std::string STR_TITLE_SETTINGS = "pick_settings";
    const std::string STR_TRACK_WATER = "track_water";
    const std::string STR_TRACK_GROUND = "track_ground";
    const std::string STR_TRACK_SKY = "track_sky";
}
//=========================================================================

LLFloaterSettingsPicker::LLFloaterSettingsPicker(LLView * owner, LLUUID initial_item_id, const LLSD &params):
    LLFloater(params),
    mOwnerHandle(),
    mActive(true),
    mContextConeOpacity(0.0f),
    mSettingItemID(initial_item_id),
    mTrackMode(TRACK_NONE),
    mImmediateFilterPermMask(PERM_NONE)
{
    mOwnerHandle = owner->getHandle();

    buildFromFile(FLOATER_DEFINITION_XML);
    setCanMinimize(false);
}


LLFloaterSettingsPicker::~LLFloaterSettingsPicker()
{

}

//-------------------------------------------------------------------------
bool LLFloaterSettingsPicker::postBuild()
{
    if (!LLFloater::postBuild())
        return false;

    std::string prefix = getString(STR_TITLE_PREFIX);
    std::string label = getString(STR_TITLE_SETTINGS);
    setTitle(prefix + " " + label);

    mFilterEdit = getChild<LLFilterEditor>(FLT_INVENTORY_SEARCH);
    mFilterEdit->setCommitCallback([this](LLUICtrl*, const LLSD& param){ onFilterEdit(param.asString()); });

    mInventoryPanel = getChild<LLInventoryPanel>(PNL_INVENTORY);
    if (mInventoryPanel)
    {
        U32 filter_types = 0x0;
        filter_types |= 0x1 << LLInventoryType::IT_SETTINGS;

        mInventoryPanel->setFilterTypes(filter_types);
        mInventoryPanel->setFilterPermMask(mImmediateFilterPermMask);

        mInventoryPanel->setSelectCallback([this](const LLFloaterSettingsPicker::itemlist_t &items, bool useraction){ onSelectionChange(items, useraction); });
        mInventoryPanel->setShowFolderState(LLInventoryFilter::SHOW_NON_EMPTY_FOLDERS);
        mInventoryPanel->setSuppressOpenItemAction(true);

        // Disable auto selecting first filtered item because it takes away
        // selection from the item set by LLTextureCtrl owning this floater.
        mInventoryPanel->getRootFolder()->setAutoSelectOverride(true);

        // don't put keyboard focus on selected item, because the selection callback
        // will assume that this was user input
        if (!mSettingItemID.isNull())
        {
            //todo: this is bad idea
            mInventoryPanel->setSelection(mSettingItemID, TAKE_FOCUS_NO);
        }
        getChild<LLView>(BTN_SELECT)->setEnabled(mSettingItemID.notNull());
    }

    mNoCopySettingsSelected = false;

    childSetAction(BTN_CANCEL, [this](LLUICtrl*, const LLSD& param){ onButtonCancel(); });
    childSetAction(BTN_SELECT, [this](LLUICtrl*, const LLSD& param){ onButtonSelect(); });

    getChild<LLPanel>(PNL_COMBO)->setVisible(mTrackMode != TRACK_NONE);

    // update permission filter once UI is fully initialized
    mSavedFolderState.setApply(false);

    return true;
}

void LLFloaterSettingsPicker::onClose(bool app_quitting)
{
    if (app_quitting)
        return;

    mCloseSignal();
    LLView *owner = mOwnerHandle.get();
    if (owner)
    {
        owner->setFocus(true);
    }
    mSettingItemID.setNull();
    mInventoryPanel->getRootFolder()->clearSelection();
}

void LLFloaterSettingsPicker::setValue(const LLSD& value)
{
    mSettingItemID = value.asUUID();
}

LLSD LLFloaterSettingsPicker::getValue() const
{
    return LLSD(mSettingItemID);
}

void LLFloaterSettingsPicker::setSettingsFilter(LLSettingsType::type_e type)
{
    U64 filter = 0xFFFFFFFFFFFFFFFF;
    if (type != LLSettingsType::ST_NONE)
    {
        filter = static_cast<S64>(0x1) << static_cast<S64>(type);
    }

    mInventoryPanel->setFilterSettingsTypes(filter);
}

void LLFloaterSettingsPicker::setTrackMode(ETrackMode mode)
{
    mTrackMode = mode;
    getChild<LLPanel>(PNL_COMBO)->setVisible(mode != TRACK_NONE);

    std::string prefix = getString(STR_TITLE_PREFIX);
    std::string label;
    if (mode != TRACK_NONE)
    {
        label = getString(STR_TITLE_TRACK);
    }
    else
    {
        label = getString(STR_TITLE_SETTINGS);
    }
    setTitle(prefix + " " + label);
}

void LLFloaterSettingsPicker::draw()
{
    LLView *owner = mOwnerHandle.get();
    static LLCachedControl<F32> max_opacity(gSavedSettings, "PickerContextOpacity", 0.4f);
    drawConeToOwner(mContextConeOpacity, max_opacity, owner);

    LLFloater::draw();
}


//=========================================================================
void LLFloaterSettingsPicker::onFilterEdit(const std::string& search_string)
{
    std::string upper_case_search_string = search_string;
    LLStringUtil::toUpper(upper_case_search_string);

    if (upper_case_search_string.empty())
    {
        if (mInventoryPanel->getFilterSubString().empty())
        {
            // current filter and new filter empty, do nothing
            return;
        }

        mSavedFolderState.setApply(true);
        mInventoryPanel->getRootFolder()->applyFunctorRecursively(mSavedFolderState);
        // add folder with current item to list of previously opened folders
        LLOpenFoldersWithSelection opener;
        mInventoryPanel->getRootFolder()->applyFunctorRecursively(opener);
        mInventoryPanel->getRootFolder()->scrollToShowSelection();

    }
    else if (mInventoryPanel->getFilterSubString().empty())
    {
        // first letter in search term, save existing folder open state
        if (!mInventoryPanel->getFilter().isNotDefault())
        {
            mSavedFolderState.setApply(false);
            mInventoryPanel->getRootFolder()->applyFunctorRecursively(mSavedFolderState);
        }
    }

    mInventoryPanel->setFilterSubString(search_string);
}

void LLFloaterSettingsPicker::onSelectionChange(const LLFloaterSettingsPicker::itemlist_t &items, bool user_action)
{
    bool is_item = false;
    LLUUID asset_id;
    if (items.size())
    {
        LLFolderViewItem* first_item = items.front();

        mNoCopySettingsSelected = false;
        if (first_item)
        {
            LLItemBridge *bridge_model = dynamic_cast<LLItemBridge *>(first_item->getViewModelItem());
            if (bridge_model && bridge_model->getItem())
            {
                if (!bridge_model->isItemCopyable())
                {
                    mNoCopySettingsSelected = true;
                }
                setSettingsItemId(bridge_model->getItem()->getUUID(), false);
                asset_id = bridge_model->getItem()->getAssetUUID();
                mViewModel->setDirty(); // *TODO: shouldn't we be using setValue() here?
                is_item = true;

                if (user_action)
                {
                    mChangeIDSignal(mSettingItemID);
                }
            }
        }
    }
    bool track_picker_enabled = mTrackMode != TRACK_NONE;

    getChild<LLView>(CMB_TRACK_SELECTION)->setEnabled(is_item && track_picker_enabled && mSettingAssetID == asset_id);
    getChild<LLView>(BTN_SELECT)->setEnabled(is_item && (!track_picker_enabled || mSettingAssetID == asset_id));
    if (track_picker_enabled && asset_id.notNull() && mSettingAssetID != asset_id)
    {
        LLUUID item_id = mSettingItemID;
        LLHandle<LLFloater> handle = getHandle();
        LLSettingsVOBase::getSettingsAsset(asset_id,
            [item_id, handle](LLUUID asset_id, LLSettingsBase::ptr_t settings, S32 status, LLExtStat) { LLFloaterSettingsPicker::onAssetLoadedCb(handle, item_id, asset_id, settings, status); });
    }
}

void LLFloaterSettingsPicker::onAssetLoadedCb(LLHandle<LLFloater> handle, LLUUID item_id, LLUUID asset_id, LLSettingsBase::ptr_t settings, S32 status)
{
    if (handle.isDead() || status)
    {
        return;
    }

    LLFloaterSettingsPicker *picker = static_cast<LLFloaterSettingsPicker *>(handle.get());

    if (picker->mSettingItemID != item_id)
    {
        return;
    }

    picker->onAssetLoaded(asset_id, settings);
}

void LLFloaterSettingsPicker::onAssetLoaded(LLUUID asset_id, LLSettingsBase::ptr_t settings)
{
    LLComboBox* track_selection = getChild<LLComboBox>(CMB_TRACK_SELECTION);
    track_selection->clear();
    track_selection->removeall();
    if (!settings)
    {
        LL_WARNS() << "Failed to load asset " << asset_id << LL_ENDL;
        return;
    }
    LLSettingsDay::ptr_t pday = std::dynamic_pointer_cast<LLSettingsDay>(settings);

    if (!pday)
    {
        LL_WARNS() << "Wrong asset type received by id " << asset_id << LL_ENDL;
        return;
    }

    if (mTrackMode == TRACK_WATER)
    {
        track_selection->add(getString(STR_TRACK_WATER), LLSD::Integer(LLSettingsDay::TRACK_WATER), ADD_TOP, true);
    }
    else if (mTrackMode == TRACK_SKY)
    {
        // track 1 always present
        track_selection->add(getString(STR_TRACK_GROUND), LLSD::Integer(LLSettingsDay::TRACK_GROUND_LEVEL), ADD_TOP, true);
        LLUIString formatted_label = getString(STR_TRACK_SKY);
        for (U32 i = 2; i < LLSettingsDay::TRACK_MAX; i++)
        {
            if (!pday->isTrackEmpty(i))
            {
                formatted_label.setArg("[NUM]", llformat("%d", i));
                track_selection->add(formatted_label.getString(), LLSD::Integer(i), ADD_TOP, true);
            }
        }
    }

    mSettingAssetID = asset_id;
    track_selection->setEnabled(true);
    track_selection->selectFirstItem();
    getChild<LLView>(BTN_SELECT)->setEnabled(true);
}

void LLFloaterSettingsPicker::onButtonCancel()
{
    closeFloater();
}

void LLFloaterSettingsPicker::onButtonSelect()
{
    if (mCommitSignal)
    {
        LLSD res;
        res["ItemId"] = mSettingItemID;
        res["Track"] = getChild<LLComboBox>(CMB_TRACK_SELECTION)->getValue();
        (*mCommitSignal)(this, res);
    }
    closeFloater();
}

bool LLFloaterSettingsPicker::handleDoubleClick(S32 x, S32 y, MASK mask)
{
    bool result = false;
    if (mSettingItemID.notNull()
        && mInventoryPanel)
    {
        S32 inventory_x = x - mInventoryPanel->getRect().mLeft;
        S32 inventory_y = y - mInventoryPanel->getRect().mBottom;
        if (mInventoryPanel->parentPointInView(inventory_x, inventory_y))
        {
            // make sure item is selected and visible
            LLFolderViewItem* item_viewp = mInventoryPanel->getItemByID(mSettingItemID);
            if (item_viewp && item_viewp->getIsCurSelection() && item_viewp->getVisible())
            {
                LLRect target_rect;
                item_viewp->localRectToOtherView(item_viewp->getLocalRect(), &target_rect, this);
                if (target_rect.pointInRect(x, y))
                {
                    // Quick-apply
                    if (mCommitSignal)
                    {
                        LLSD res;
                        res["ItemId"] = mSettingItemID;
                        res["Track"] = getChild<LLComboBox>(CMB_TRACK_SELECTION)->getValue();
                        (*mCommitSignal)(this, res);
                    }
                    closeFloater();
                    // hit inside panel on selected item, double click should do nothing
                    result = true;
                }
            }
        }
    }

    if (!result)
    {
        result = LLFloater::handleDoubleClick(x, y, mask);
    }
    return result;
}

bool LLFloaterSettingsPicker::handleKeyHere(KEY key, MASK mask)
{
    if ((key == KEY_RETURN) && (mask == MASK_NONE))
    {
        LLFolderViewItem* item_viewp = mInventoryPanel->getItemByID(mSettingItemID);
        if (item_viewp && item_viewp->getIsCurSelection() && item_viewp->getVisible())
        {
            // Quick-apply
            if (mCommitSignal)
            {
                LLSD res;
                res["ItemId"] = mSettingItemID;
                res["Track"] = getChild<LLComboBox>(CMB_TRACK_SELECTION)->getValue();
                (*mCommitSignal)(this, res);
            }
            closeFloater();
            return true;
        }
    }

    return LLFloater::handleKeyHere(key, mask);
}

void LLFloaterSettingsPicker::onFocusLost()
{
    if (isInVisibleChain())
    {
        closeFloater();
    }
}

//=========================================================================
void LLFloaterSettingsPicker::setActive(bool active)
{
    mActive = active;
}

void LLFloaterSettingsPicker::setSettingsItemId(const LLUUID &settings_id, bool set_selection)
{
    if (mSettingItemID != settings_id && mActive)
    {
        mNoCopySettingsSelected = false;
        mViewModel->setDirty(); // *TODO: shouldn't we be using setValue() here?
        mSettingItemID = settings_id;
        if (mSettingItemID.isNull())
        {
            mInventoryPanel->getRootFolder()->clearSelection();
        }
        else
        {
            LLInventoryItem* itemp = gInventory.getItem(settings_id);
            if (itemp && !itemp->getPermissions().allowCopyBy(gAgent.getID()))
            {
                mNoCopySettingsSelected = true;
            }
        }

        if (set_selection)
        {
            mInventoryPanel->setSelection(settings_id, TAKE_FOCUS_NO);
        }
    }
}

LLInventoryItem* LLFloaterSettingsPicker::findItem(const LLUUID& asset_id, bool copyable_only, bool ignore_library)
{
    LLViewerInventoryCategory::cat_array_t cats;
    LLViewerInventoryItem::item_array_t items;
    LLAssetIDMatches asset_id_matches(asset_id);

    gInventory.collectDescendentsIf(LLUUID::null,
        cats,
        items,
        LLInventoryModel::INCLUDE_TRASH,
        asset_id_matches);

    if (items.size())
    {
        // search for copyable version first
        for (S32 i = 0; i < items.size(); i++)
        {
            LLInventoryItem* itemp = items[i];
            LLPermissions item_permissions = itemp->getPermissions();
            if (item_permissions.allowCopyBy(gAgent.getID(), gAgent.getGroupID()))
            {
                if(!ignore_library || !gInventory.isObjectDescendentOf(itemp->getUUID(),gInventory.getLibraryRootFolderID()))
                {
                    return itemp;
                }
            }
        }
        // otherwise just return first instance, unless copyable requested
        if (copyable_only)
        {
            return nullptr;
        }
        else
        {
            if(!ignore_library || !gInventory.isObjectDescendentOf(items[0]->getUUID(),gInventory.getLibraryRootFolderID()))
            {
                return items[0];
            }
        }
    }

    return nullptr;
}