tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sfx2 / source / sidebar / ContextList.cxx
blob9f171644717a50fd52a1cb60c534a4c8feffb41d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <sidebar/ContextList.hxx>
20 #include <sfx2/sidebar/Context.hxx>
22 namespace sfx2::sidebar {
24 ContextList::ContextList()
28 const ContextList::Entry* ContextList::GetMatch (const Context& rContext) const
30 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
31 if (iEntry != maEntries.end())
32 return &*iEntry;
33 else
34 return nullptr;
37 ContextList::Entry* ContextList::GetMatch (const Context& rContext)
39 const ::std::vector<Entry>::const_iterator iEntry = FindBestMatch(rContext);
40 if (iEntry != maEntries.end())
41 return const_cast<Entry*>(&*iEntry);
42 else
43 return nullptr;
46 ::std::vector<ContextList::Entry>::const_iterator ContextList::FindBestMatch (const Context& rContext) const
48 sal_Int32 nBestMatch (Context::NoMatch);
49 ::std::vector<Entry>::const_iterator iBestMatch (maEntries.end());
51 for (::std::vector<Entry>::const_iterator
52 iEntry(maEntries.begin()),
53 iEnd(maEntries.end());
54 iEntry!=iEnd;
55 ++iEntry)
57 const sal_Int32 nMatch (rContext.EvaluateMatch(iEntry->maContext));
58 if (nMatch < nBestMatch)
60 nBestMatch = nMatch;
61 iBestMatch = iEntry;
63 if (nBestMatch == Context::OptimalMatch)
64 return iEntry;
67 return iBestMatch;
70 void ContextList::AddContextDescription (
71 const Context& rContext,
72 const bool bIsInitiallyVisible,
73 const OUString& rsMenuCommand)
75 maEntries.emplace_back();
76 maEntries.back().maContext = rContext;
77 maEntries.back().mbIsInitiallyVisible = bIsInitiallyVisible;
78 maEntries.back().msMenuCommand = rsMenuCommand;
81 void ContextList::ToggleVisibilityForContext( const Context &rContext, const bool bVisible)
83 ContextList::Entry *pEntry = GetMatch( rContext );
85 if ( !pEntry )
86 return;
88 const sal_Int32 nMatch( rContext.EvaluateMatch( pEntry->maContext ) );
90 if ( nMatch & Context::ApplicationWildcardMatch )
92 // Create a separate context list entry for this app if 'any'
93 // is the only context that matches. Toggling the visibility
94 // for 'any' would change it for all apps, not just this one
95 AddContextDescription( rContext, bVisible, OUString() );
97 else if ( nMatch == Context::OptimalMatch || nMatch == Context::ContextWildcardMatch )
98 pEntry->mbIsInitiallyVisible = bVisible;
101 } // end of namespace sfx2::sidebar
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */