%% start of file `moderncvskillmatrix.sty'.
%% Copyright 2021 David Seus (cryptointerest@posteo.de).
%
% This package provides a skill matrix template for the moderncv package.
% Some recruiting firms require applicants to rate their skills, computer 
% skills, management tools, or similar in a table involving a grafical 
% illustration of their skill level. This package implements this idea 
% and provides the following new commands to use within the moderncv 
% package:
% 
% \cvskill{<1-5>}
%   Illustrate skill level with little colored boxes.
%   By default five skill levels are predefined.   
% 
%   Input: 
%       input_1:    integer between 0 and 5
%   Example usages: 
%       \cvskill{3}
% 
% 
% \cvskilllegend[<post_padding>][<first_level>][<second_level>][<third_level>][<fourth_level>][<fifth_level>]{<name>}
%   Print legend table explaining the meaning of \cvskill{1}...\cvskill{5}.
% 
%   Input:
%       input_1 (optional): padding length appended to the legend, default: <0.25em>
%       input_2 (optional): string describing meaning of \cvskill{1}, default: <basic knowledge>
%       input_3 (optional): string describing meaning of \cvskill{2}, default: <intermediate knowledge with some project experience>
%       input_4 (optional): string describing meaning of \cvskill{3}, default: <extensive project experience>
%       input_5 (optional): string describing meaning of \cvskill{4}, default: <deepened expert knowledge>
%       input_6 (optional): string describing meaning of \cvskill{5}, default: <expert/guru>
%       input_7:            string giving a name to the legend, default: <>
%   Example usages:
%       \cvskilllegend{}    print default legend
%       \cvskilllegend[0.5em]{Legend}   print default legend, but adjust post padding and display the word ``Legend''.
%       \cvskilllegend[0.2em][Grundkenntnisse][Grundkenntnisse und eigene Erfahrung in Projekten][Umfangreiche Erfahrung in Projekten][Vertiefte Expertenkenntnisse][Experte/Guru]{Legende}     Fully adjust legend to German example
% 
%   Note:   
%   The design of the table is such that the <second_level> entry stands in the first column but 
%   occupies rows two and three. This has been done to accomodate english and german defaults.
%   TODO: Add more generic table without this spciality.
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License version 1.3c,
% available at http://www.latex-project.org/lppl/.


%-------------------------------------------------------------------------------
%                identification
%-------------------------------------------------------------------------------
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{moderncvskillmatrix}[2021/01/18 v2.1.0 modern curriculum vitae and letter skill matrix]

% The definitions need to be adjusted depending on which moderncvbody<i-v>.sty style is usde. 
% body type options: "moderncvbodyi" (default), "moderncvbodyii", "moderncvbodyiii", "moderncvbodyiv" or "moderncvbodyv" 
\@initializeif{\if@moderncvbodyi}\@moderncvbodyifalse
\DeclareOption{moderncvbodyi}{\@moderncvbodyitrue\@moderncvbodyiifalse\@moderncvbodyiiifalse\@moderncvbodyivfalse\@moderncvbodyvfalse}
\@initializeif{\if@moderncvbodyii}\@moderncvbodyiifalse
\DeclareOption{moderncvbodyii}{\@moderncvbodyifalse\@moderncvbodyiitrue\@moderncvbodyiiifalse\@moderncvbodyivfalse\@moderncvbodyvfalse}
\@initializeif{\if@moderncvbodyiii}\@moderncvbodyiiifalse
\DeclareOption{moderncvbodyiii}{\@moderncvbodyifalse\@moderncvbodyiifalse\@moderncvbodyiiitrue\@moderncvbodyivfalse\@moderncvbodyvfalse}
\@initializeif{\if@moderncvbodyiv}\@moderncvbodyivfalse
\DeclareOption{moderncvbodyiv}{\@moderncvbodyifalse\@moderncvbodyiifalse\@moderncvbodyiiifalse\@moderncvbodyivtrue\@moderncvbodyvfalse}
\@initializeif{\if@moderncvbodyv}\@moderncvbodyvfalse
\DeclareOption{moderncvbodyv}{\@moderncvbodyifalse\@moderncvbodyiifalse\@moderncvbodyiiifalse\@moderncvbodyivfalse\@moderncvbodyvtrue}

\DeclareOption*{}% avoid choking on unknown options
\ExecuteOptions{moderncvbodyi}
\ProcessOptions*\relax% \ProcessOptions* processes the options in the order provided (i.e., with the later ones possibly overriding the former ones), while \ProcessOptions processes them in the order of the package

%-------------------------------------------------------------------------------
%                required packages
%-------------------------------------------------------------------------------
\RequirePackage{tikz}
\RequirePackage{multirow}
% package arydshln is needed for the dashed lines but is incompatible with fancy style
\if@moderncvbodyv%
%     \RequirePackage{arydshln} % incompatible with fancy style
\else%
    \RequirePackage{arydshln} % incompatible with fancy style
\fi

% %-------------------------------------------------------------------------------
% %                \cvskill command
% %-------------------------------------------------------------------------------
% The code for the Skilllevel illustration with the little boxes.
% This is idea stolen from the limecv package, see
% https://github.com/opieters/limecv.git
\@initializelength{\cvSkill@RectangleSize}
\setlength{\cvSkill@RectangleSize}{1.2ex}
\newcount\my@repeat@count
\DeclareDocumentCommand{\cvskill}{m}{%
%   Illustrate skill level with little colored boxes.
%   By default five skill levels are predefined.   
% 
%   Input: integer between 0 and 5
%   Example usage: \cvskill{3}
    \begingroup
        \my@repeat@count=\z@
        \@whilenum\my@repeat@count<#1\do{\tikz\filldraw[color1] (0, 0) rectangle (\cvSkill@RectangleSize, \cvSkill@RectangleSize);\advance%
        \my@repeat@count\@ne\,}%
        \my@repeat@count=\numexpr5-\z@\relax
        \@whilenum\my@repeat@count>#1\do{\tikz\filldraw[color2!30] (0, 0) rectangle (\cvSkill@RectangleSize, \cvSkill@RectangleSize);\advance%
        \my@repeat@count\m@ne\,}%
    \endgroup
}

% %-------------------------------------------------------------------------------
% %                \recomputecvskillmatrixlengths
% %-------------------------------------------------------------------------------

% initialise lengths needed for the skillmatrix
\@initializelength{\cvskill@width}
\@initializelength{\cvskill@descriptorwidth}
\@initializelength{\cvskill@experiencewidth}
\@initializelength{\skillmatrix@columnwidth}
\@initializelength{\skillmatrix@commentwidth}
\@initializelength{\skillmatrix@padding}
\@initializelength{\skillmatrix@hintscolumnwidth}
\@initializelength{\skillmatrix@bodylength}
\@initializelength{\cvskilllegend@leftdescriptorwidth}
\@initializelength{\cvskilllegend@rightdescriptorwidth}
\@initializelength{\skilllegend@hintscolumnwidth}
\@initializelength{\skilllegend@padding}
\@initializelength{\skilllegend@bodylength}

% \separatorrulewidth is defined in moderncvbodyv.sty and we need to 
% define it to not throw an error in the other cases
\if@moderncvbodyv%
%     \RequirePackage{arydshln} % incompatible with fancy style
\else%
    \@initializelength{\separatorrulewidth}
    \setlength{\separatorrulewidth}{1ex}
\fi

\DeclareDocumentCommand{\skilllegend@leftdesriptorfactor}{}{}%
%% DEFINITION \recomputecvskillmatrixlengths
% declare the command \recomputecvskillmatrixlengths empty
\DeclareDocumentCommand{\recomputecvskillmatrixlengths}{}{}%
% the command gets redifined depending on which option is given
\if@moderncvbodyi%
    \RenewDocumentCommand{\recomputecvskillmatrixlengths}{}{%
        \setlength{\skillmatrix@padding}{1ex}%
        \setlength{\skilllegend@padding}{0.25ex}%
        \setlength{\skillmatrix@hintscolumnwidth}{\hintscolumnwidth}%
        \setlength{\skilllegend@hintscolumnwidth}{\hintscolumnwidth}%
        \setlength{\cvskill@width}{\widthof{\cvskill{5}}}%
        \setlength{\cvskill@experiencewidth}{\widthof{``Year''}}%
        \setlength{\skillmatrix@bodylength}{\maincolumnwidth}%
        \setlength{\skillmatrix@columnwidth}{0.45\skillmatrix@bodylength}%
        \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
        \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
        \setlength{\skilllegend@bodylength}{\skillmatrix@bodylength}%
        \RenewDocumentCommand{\skilllegend@leftdesriptorfactor}{}{0.5}%
        \setlength{\cvskilllegend@leftdescriptorwidth}{\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
        \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
    }
\fi

\if@moderncvbodyiii%
    \RenewDocumentCommand{\recomputecvskillmatrixlengths}{}{%
        \setlength{\skillmatrix@padding}{1ex}%
        \setlength{\skilllegend@padding}{0.25ex}%
        \setlength{\separatorcolumnwidth}{\skillmatrix@padding}%
        \setlength{\skillmatrix@hintscolumnwidth}{\widthof{``Language''}}%
        \setlength{\skilllegend@hintscolumnwidth}{\skillmatrix@hintscolumnwidth}%
        \setlength{\cvskill@width}{\widthof{\cvskill{5}}}%
        \setlength{\cvskill@experiencewidth}{\widthof{``Year''}}%
        \setlength{\skillmatrix@bodylength}{\maincolumnwidth-\skillmatrix@hintscolumnwidth-\separatorcolumnwidth}%
        \setlength{\skillmatrix@columnwidth}{0.45\skillmatrix@bodylength}%
        \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
        \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
        \setlength{\skilllegend@bodylength}{\skillmatrix@bodylength}%
        \RenewDocumentCommand{\skilllegend@leftdesriptorfactor}{}{0.5}%
        \setlength{\cvskilllegend@leftdescriptorwidth}{\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
        \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
    }
\fi

\if@moderncvbodyiv%
    \RenewDocumentCommand{\recomputecvskillmatrixlengths}{}{%
        \setlength{\skillmatrix@padding}{1ex}
        \setlength{\skilllegend@padding}{0.25ex}
        \setlength{\separatorcolumnwidth}{\skillmatrix@padding}
        \setlength{\skillmatrix@hintscolumnwidth}{\widthof{``Languagi''}}
        \setlength{\skilllegend@hintscolumnwidth}{0ex}%\skillmatrix@hintscolumnwidth
        \setlength{\cvskill@width}{\widthof{\cvskill{5}}}
        \setlength{\cvskill@experiencewidth}{\widthof{``Year''}}
        \setlength{\skillmatrix@bodylength}{\maincolumnwidth-\skillmatrix@hintscolumnwidth-\separatorcolumnwidth}
        \setlength{\skillmatrix@columnwidth}{0.45\skillmatrix@bodylength}%
        \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}
        \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
        \setlength{\skilllegend@bodylength}{\maincolumnwidth}
        \RenewDocumentCommand{\skilllegend@leftdesriptorfactor}{}{0.45}%
        \setlength{\cvskilllegend@leftdescriptorwidth}{\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}
        \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}
    }
\fi

\if@moderncvbodyv%
    \RenewDocumentCommand{\recomputecvskillmatrixlengths}{}{%
        \setlength{\skillmatrix@padding}{1ex}%
        \setlength{\skilllegend@padding}{0.25ex}%
        \setlength{\skillmatrix@hintscolumnwidth}{\widthof{``Languages''}}%
        \setlength{\skilllegend@hintscolumnwidth}{\skillmatrix@hintscolumnwidth}%
        \setlength{\cvskill@width}{\widthof{\cvskill{5}}}%
        \setlength{\cvskill@experiencewidth}{\widthof{``Year''}}%
        \setlength{\skillmatrix@bodylength}{\maincolumnwidth-\skillmatrix@hintscolumnwidth-\skillmatrix@padding}%\skillmatrix@hintscolumnwidth
        \setlength{\skillmatrix@columnwidth}{0.45\skillmatrix@bodylength}%
        \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
        \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
        \setlength{\skilllegend@bodylength}{\maincolumnwidth}%
        \RenewDocumentCommand{\skilllegend@leftdesriptorfactor}{}{0.48}%
        \setlength{\cvskilllegend@leftdescriptorwidth}{\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
        \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
    }
\fi

% command to adjust the width of the columns of the skillmatrix. 
% \setcvskillcolumns[<width>][<factor>][<exp_width>], where <width> is a length smaller than 
% \textwidth influencing the width of the first column. 
% 0 < <factor> < 1 is a factor influencing the width of the skill column, aka \skillmatrix@columnwidth,
% and <exp_width> is a width smaller than \textwidth resetting the width (\cvskill@experiencewidth) of the years column.
\DeclareDocumentCommand{\setcvskillcolumns}{+O{} +O{} +O{}}{}%
\RenewDocumentCommand{\setcvskillcolumns}{+O{\skillmatrix@hintscolumnwidth} +O{0.45} +O{\cvskill@experiencewidth}}{%
    \def\arg@new@hintscolumnwidth{#1}% <-- all these terminal % signs are necessary for the fancy style to not show weird spaces!!!
    \def\arg@new@bodyLengthFactor{#2}% 
    \def\arg@new@experienceWidth{#3}% 
    % Check for empty arguments. Defaults are given. Thus a call of \setcvskillcolumns 
    % without any arguments leads to nonempty arguments \arg@new@hintscolumnwidth and
    % \def\arg@new@bodyLengthFactor{#2}. However, we need to take care of calls like 
    % \setcvskillcolumns[], \setcvskillcolumns[][], \setcvskillcolumns[][][] or even 
    % \setcvskillcolumns[<somelength>][], \setcvskillcolumns[][<somefactor>]  \setcvskillcolumns[][][<length>]
    \ifdefempty{\arg@new@hintscolumnwidth}{%
        % Case \setcvskillcolumns[], \setcvskillcolumns[][] or \setcvskillcolumns[][<somefactor>]
        \ifdefempty{\arg@new@bodyLengthFactor}{%
            % Case \setcvskillcolumns[][] do nothing here and check if third argument is empty
            \ifdefempty{\arg@new@experienceWidth}{%
                % Case \setcvskillcolumns[][][] do nothing here
            }{%
                % Case \setcvskillcolumns[][][<length>]. reset \cvskill@experiencewidth and
                % \cvskill@descriptorwidth accordingly
                \setlength{\cvskill@experiencewidth}{\arg@new@experienceWidth}%
                \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
            }%
        }{%
            % Case \setcvskillcolumns[][<somefactor>], \setcvskillcolumns[][<somefactor>][<possilly length>]
            \setlength{\skillmatrix@columnwidth}{\arg@new@bodyLengthFactor\skillmatrix@bodylength}%
            \ifdefempty{\arg@new@experienceWidth}{%
                % Case \setcvskillcolumns[][<somefactor>][] do nothing here
            }{%
                % Case \setcvskillcolumns[][<somefactor>][<length>]. reset \cvskill@experiencewidth and
                % \cvskill@descriptorwidth accordingly
                \setlength{\cvskill@experiencewidth}{\arg@new@experienceWidth}%
                \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
            }%            
            \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
            \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
        }%
        % Case \setcvskillcolumns[] nothing needs to be done here recalculate lengths affected by the change
    }{% 
        % Case \setcvskillcolumns, \setcvskillcolumns[<width>], \setcvskillcolumns[<width>][] 
        % or \setcvskillcolumns[<width>][<somefactor>]
        \setlength{\skillmatrix@hintscolumnwidth}{\arg@new@hintscolumnwidth}%
        \setlength{\skillmatrix@bodylength}{\maincolumnwidth-\skillmatrix@hintscolumnwidth-\separatorcolumnwidth}%
        % in case second argument is given but left empty use default
        \ifdefempty{\arg@new@bodyLengthFactor}{%
            % Case \setcvskillcolumns[<width>][] do nothing here and use default
            % \skillmatrix@columnwidth and check third argument
            \ifdefempty{\arg@new@experienceWidth}{%
                % Case \setcvskillcolumns[<width>][][] do nothing here
            }{%
                % Case \setcvskillcolumns[<width>][][<length>]. reset \cvskill@experiencewidth and
                % \cvskill@descriptorwidth accordingly
                \setlength{\cvskill@experiencewidth}{\arg@new@experienceWidth}%
%                 \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
            }%
        }{%
            % Case \setcvskillcolumns, \setcvskillcolumns[<width>], \setcvskillcolumns[<width>][<somefactor>]
            \setlength{\skillmatrix@columnwidth}{\arg@new@bodyLengthFactor\skillmatrix@bodylength}%
            \ifdefempty{\arg@new@experienceWidth}{%
                % Case \setcvskillcolumns[<width>][<somefactor>][] do nothing here
            }{%
                % Case \setcvskillcolumns[<width>][<somefactor>][<length>]. reset \cvskill@experiencewidth and
                % \cvskill@descriptorwidth accordingly
                \setlength{\cvskill@experiencewidth}{\arg@new@experienceWidth}%
%                 \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
            }%
        }%
        \setlength{\cvskill@descriptorwidth}{\skillmatrix@columnwidth-\cvskill@width-\cvskill@experiencewidth}%
        \setlength{\skillmatrix@commentwidth}{\skillmatrix@bodylength-\skillmatrix@columnwidth-3\skillmatrix@padding}%
    }%
}%
% 
% command to adjust the width of the columns of the skilllegend. 
% \setcvskilllegendcolumns[<width>][<factor>], where <width> is a length smaller than 
% \textwidth influencing the width of the first column where depending on the style the legend_string 
% gets printed. In case the string is left empty adjusting this width allows moving the legend horizontally. 
% 0 < <factor> < 1 is a factor influencing the width of the left legend descritor column, aka \cvskilllegend@leftdescriptorwidth.
% \cvskilllegend@rightdescriptorwidth is influenced by the factor 1-<factor>
\DeclareDocumentCommand{\setcvskilllegendcolumns}{+O{} +O{}}{}%
\RenewDocumentCommand{\setcvskilllegendcolumns}{+O{\skilllegend@hintscolumnwidth} +O{0.5}}{%
    \def\arg@new@legend@hintscolumnwidth{#1}%
    \def\arg@new@legend@leftDescriptorFactor{#2}%
    % Check for empty arguments. See explanation above
    \ifdefempty{\arg@new@legend@hintscolumnwidth}{%
        % Case \setcvskilllegendcolumns[], \setcvskilllegendcolumns[][] or \setcvskilllegendcolumns[][<somefactor>]
        \ifdefempty{\arg@new@legend@leftDescriptorFactor}{%
%             % Case \setcvskilllegendcolumns[][] do nothing here, i.e. leave default values unaltered
        }{%
            % Case \setcvskilllegendcolumns[][<somefactor>], \setcvskilllegendcolumns[][<somefactor>]
            \setlength{\cvskilllegend@leftdescriptorwidth}{\arg@new@legend@leftDescriptorFactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
            \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\arg@new@legend@leftDescriptorFactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
        }%
%         % Case \setcvskilllegendcolumns[] nothing needs to be done here i.e. leave default values unaltered
    }{% 
        % Case \setcvskilllegendcolumns, \setcvskilllegendcolumns[<width>], \setcvskilllegendcolumns[<width>][] 
        % or \setcvskilllegendcolumns[<width>][<somefactor>]
        \setlength{\skilllegend@hintscolumnwidth}{\arg@new@legend@hintscolumnwidth}%
        \if@moderncvbodyi%
            \setlength{\skilllegend@bodylength}{\textwidth-\skilllegend@hintscolumnwidth-\separatorcolumnwidth}%
        \fi%
        \if@moderncvbodyiii%
            \setlength{\skilllegend@bodylength}{\textwidth-\skilllegend@hintscolumnwidth-\separatorcolumnwidth}%
        \fi%
        \if@moderncvbodyiv%
            \setlength{\skilllegend@bodylength}{\maincolumnwidth-\skilllegend@hintscolumnwidth-\separatorcolumnwidth}%
        \fi%
        % in case second argument is given but left empty use default
        \ifdefempty{\arg@new@legend@leftDescriptorFactor}{%
            % Case \setcvskilllegendcolumns[<width>][] do nothing here and leave default values unaltered
        }{%
            % Case \setcvskilllegendcolumns, \setcvskilllegendcolumns[<width>], \setcvskilllegendcolumns[<width>][<somefactor>]
            \RenewDocumentCommand{\skilllegend@leftdesriptorfactor}{}{\arg@new@legend@leftDescriptorFactor}%
        }%
        \setlength{\cvskilllegend@leftdescriptorwidth}{\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
        \setlength{\cvskilllegend@rightdescriptorwidth}{\skilllegend@bodylength-\skilllegend@leftdesriptorfactor\skilllegend@bodylength-\cvskill@width-\skillmatrix@padding-2\skilllegend@padding}%
    }%
}%
% 
% %-------------------------------------------------------------------------------
% %                \cvskilllegend 
% %-------------------------------------------------------------------------------
% include a header line for the skill matrix. 
% \cvskilllegend[<post_padding>][<first_level>][<second_level>][<third_level>][<fourth_level>][<fifth_level>]{<name>}
\NewDocumentCommand\skillLegend@FontSize{}{\scriptsize}
\DeclareDocumentCommand\cvskilllegend{s +O{} +O{} +O{} +O{} +O{} +O{} +m}{}%
%   Print legend table explaining the meaning of \cvskill{1}...\cvskill{5}.
% 
%   Input:
%       input_1 (optional): padding length appended to the legend, default: <0.25em>
%       input_2 (optional): string describing meaning of \cvskill{1}, default: <basic knowledge>
%       input_3 (optional): string describing meaning of \cvskill{2}, default: <intermediate knowledge with some project experience>
%       input_4 (optional): string describing meaning of \cvskill{3}, default: <extensive project experience>
%       input_5 (optional): string describing meaning of \cvskill{4}, default: <deepened expert knowledge>
%       input_6 (optional): string describing meaning of \cvskill{5}, default: <expert/guru>
%       input_7:            string giving a name to the legend, default: <>
%   Example usages:
%       \cvskilllegend{}    print default legend
%       \cvskilllegend[0.5em]{Legend}   print default legend, but adjust post padding and display the word ``Legend''.
%       \cvskilllegend[0.2em][Grundkenntnisse][Grundkenntnisse und eigene Erfahrung in Projekten][Umfangreiche Erfahrung in Projekten][Vertiefte Expertenkenntnisse][Experte/Guru]{Legende}     Fully adjust legend to German example
% 
%   Note:   
%   The design of the table is such that the <second_level> entry stands in the first column but 
%   occupies rows two and three. This has been done to accomodate english and german defaults.
%   TODO: Add more generic table without this spciality.
% 
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License version 1.3c,
% available at http://www.latex-project.org/lppl/.

% declare default legend entries
\NewDocumentCommand\skillLegend@defaultLevelOne{}{basic knowledge}
\NewDocumentCommand\skillLegend@defaultLevelTwo{}{intermediate knowledge with some project experience}
\NewDocumentCommand\skillLegend@defaultLevelThree{}{extensive project experience}
\NewDocumentCommand\skillLegend@defaultLevelFour{}{deepened expert knowledge}
\NewDocumentCommand\skillLegend@defaultLevelFive{}{expert/guru}
% Redefinition of \cvskilllegend valid for moderncvbodyi, moderncvbodyii, moderncvbodyiii
\RenewDocumentCommand\cvskilllegend{s +O{.25em} +O{\skillLegend@defaultLevelOne} +O{\skillLegend@defaultLevelTwo} +O{\skillLegend@defaultLevelThree} +O{\skillLegend@defaultLevelFour} +O{\skillLegend@defaultLevelFive} +m}{%
    \IfBooleanTF#1{% if a star is given, add dashed line
        \begingroup%
        \arrayrulecolor{color1}%
        \begin{tabular}{@{}p{\skilllegend@hintscolumnwidth}%
                        @{\hspace{\separatorcolumnwidth}}%
                        p{\cvskill@width}@{\hspace{\skilllegend@padding}};{.6pt/1pt}%%
                        p{\skilllegend@padding}p{\cvskilllegend@leftdescriptorwidth}@{}@{\hspace{2\skillmatrix@padding}}%
                        p{\cvskill@width}@{\hspace{\skilllegend@padding}};{.6pt/1pt}%
                        p{\skilllegend@padding}%
                        p{\cvskilllegend@rightdescriptorwidth}@{}}%
            \raggedleft\hintstyle{#8}  & \cvskill{1}& & {\skillLegend@FontSize #3} & \cvskill{3}& &{\skillLegend@FontSize #5 } \\%
                        %
                    & \cvskill{2} & & \multirow{2}{\cvskilllegend@leftdescriptorwidth}{{\skillLegend@FontSize #4}} & \cvskill{4}& & {\skillLegend@FontSize #6 } \\%
                %         
                    &  & &  & \cvskill{5}& & {\skillLegend@FontSize #7 }%
        \end{tabular}%
        \endgroup
        \par\addvspace{#2}}{%
        % if no star is given, do not add dashed line
        \begin{tabular}{@{}p{\skilllegend@hintscolumnwidth}%
                        @{\hspace{\separatorcolumnwidth}}%
                        p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                        p{\cvskilllegend@leftdescriptorwidth}@{\hspace{2\skillmatrix@padding}}%
                        p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                        p{\cvskilllegend@rightdescriptorwidth}@{}}%
            \raggedleft\hintstyle{#8}  & \cvskill{1}\, & \,{\skillLegend@FontSize #3} & \cvskill{3}\, &\,{\skillLegend@FontSize #5 } \\%
                        %
                    & \cvskill{2}\, & \,\multirow{2}{\cvskilllegend@leftdescriptorwidth}{{\skillLegend@FontSize #4}} & \cvskill{4}\,  &\,{\skillLegend@FontSize #6 } \\%
                %         
                    &  &   & \cvskill{5}\, &\,{\skillLegend@FontSize #7 }%
        \end{tabular}%
        \par\addvspace{#2}%
    }%
}%
% Redefinition of \cvskilllegend valid for moderncvbodyiv
\if@moderncvbodyiv%
    \RenewDocumentCommand\cvskilllegend{s +O{.25em} +O{\skillLegend@defaultLevelOne} +O{\skillLegend@defaultLevelTwo} +O{\skillLegend@defaultLevelThree} +O{\skillLegend@defaultLevelFour} +O{\skillLegend@defaultLevelFive} +m}{%
        \def\arg@legendString{#8}%
        \ifdefempty{\arg@legendString}{%
        }{%
            \cvitem[0.25em]{\hintstyle{#8}}{}%
        }%
        \IfBooleanTF#1{% if a star is given, add dashed line
            \begingroup
            \arrayrulecolor{color1}
            \begin{tabular}{@{}p{\skilllegend@hintscolumnwidth}
                            @{\hspace{\separatorcolumnwidth}}p{\cvskill@width}@{\hspace{\skilllegend@padding}};{.6pt/1pt}%
                            p{\skilllegend@padding}%
                            p{\cvskilllegend@leftdescriptorwidth}@{\hspace{2\skillmatrix@padding}}%
                            p{\cvskill@width}@{\hspace{\skilllegend@padding}};{.6pt/1pt}%
                            p{\skilllegend@padding}p{\cvskilllegend@rightdescriptorwidth}@{}}%
                \raggedleft\hintstyle{}  & \cvskill{1} & & {\skillLegend@FontSize #3} & \cvskill{3} & & {\skillLegend@FontSize #5 } \\
                            %
                        & \cvskill{2} & & \multirow{2}{\cvskilllegend@leftdescriptorwidth}{{\skillLegend@FontSize #4}} & \cvskill{4} &  &{\skillLegend@FontSize #6 } \\
                    %         
                        & & &   & \cvskill{5}& & {\skillLegend@FontSize #7 } 
            \end{tabular}%
            \endgroup
            \par\addvspace{#2}}{
            % if no star is given, do not add dashed line
            \begin{tabular}{@{}p{\skilllegend@hintscolumnwidth}
                            @{\hspace{\separatorcolumnwidth}}p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                            p{\skilllegend@padding}%
                            p{\cvskilllegend@leftdescriptorwidth}@{\hspace{2\skillmatrix@padding}}%
                            p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                            p{\skilllegend@padding}p{\cvskilllegend@rightdescriptorwidth}@{}}%
                \raggedleft\hintstyle{}  & \cvskill{1} & & {\skillLegend@FontSize #3} & \cvskill{3} & & {\skillLegend@FontSize #5 } \\
                            %
                        & \cvskill{2} & & \multirow{2}{\cvskilllegend@leftdescriptorwidth}{{\skillLegend@FontSize #4}} & \cvskill{4} &  &{\skillLegend@FontSize #6 } \\
                    %         
                        & & &   & \cvskill{5}& & {\skillLegend@FontSize #7 } 
            \end{tabular}%
            \par\addvspace{#2}
        }
    }
\fi
% Redefinition of \cvskilllegend valid for moderncvbodyv
\if@moderncvbodyv%
    \DeclareDocumentCommand\@starIndependentTabular{}{}%
        \RenewDocumentCommand\cvskilllegend{s +O{.25em} +O{\skillLegend@defaultLevelOne} +O{\skillLegend@defaultLevelTwo} +O{\skillLegend@defaultLevelThree} +O{\skillLegend@defaultLevelFour} +O{\skillLegend@defaultLevelFive} +m}{%
        % check whether Argument #8 is given and if so provide it as cvitem
        \def\arg@legendString{#8}%
        \ifdefempty{\arg@legendString}{%
            \vspace*{-\separatorrulewidth}% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
        }{%
            \cvitem[-0.5em]{#8}{}%
        }%
        \arrayrulecolor{color1}%
        \setlength\arrayrulewidth{\separatorrulewidth}%
        \RenewDocumentCommand{\@starIndependentTabular}{}{%
            \begingroup%
%             \renewcommand{\arraystretch}{1.0}%
            \begin{tabular}[t]{@{}p{\hintscolumnwidth}%\skilllegend@hintscolumnwidth
                            @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}%
                            p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                            p{\skilllegend@padding}%
                            p{\cvskilllegend@leftdescriptorwidth}@{\hspace{2\skillmatrix@padding}}%
                            p{\cvskill@width}@{\hspace{\skilllegend@padding}}%
                            p{\skilllegend@padding}%
                            p{\cvskilllegend@rightdescriptorwidth}@{}}%
                \@moderncvstrut{4pt}{16pt}  & \cvskill{1}& & {\skillLegend@FontSize #3} & \cvskill{3}& & {\skillLegend@FontSize #5 } \\%
                            %
                        & \cvskill{2}& & \multirow{2}{\cvskilllegend@leftdescriptorwidth}{{\skillLegend@FontSize #4}} & \cvskill{4}&  & {\skillLegend@FontSize #6 } \\%
                    %         
                        & &  &   & \cvskill{5}& & {\skillLegend@FontSize #7 } \\[#2]% the spacing needs to be inside the cell for the vertical rule to extend correctly
            \end{tabular}%
            \endgroup%
            \par\@aftersectionfalse\ignorespaces%    
        }%
        % because of this weird style and the position of the parameter [#2], the dashed lines of the other version look bad. So no lines. 
        \IfBooleanTF#1{%
                \@starIndependentTabular%
            }{% 
                \@starIndependentTabular%
        }%
    }%
\fi

% %-------------------------------------------------------------------------------
% %                \cvSkillMatrix@HeadFont 
% %-------------------------------------------------------------------------------
\NewDocumentCommand\cvSkillMatrix@HeadFont{}{\normalfont}
\DeclareDocumentCommand\cvskillhead{+O{} +O{} +O{} +O{} +O{}}{}%

\NewDocumentCommand\skillMatrix@HeaderDefaultOne{}{Level}
\NewDocumentCommand\skillMatrix@HeaderDefaultTwo{}{Skill}
\NewDocumentCommand\skillMatrix@HeaderDefaultThree{}{Years}
\NewDocumentCommand\skillMatrix@HeaderDefaultFour{}{Comment}
% The following definition is valid for moderncvbodyi, moderncvbodyii, moderncvbodyiii, moderncvbodyiv
\RenewDocumentCommand\cvskillhead{O{.25em} +O{\skillMatrix@HeaderDefaultOne} +O{\skillMatrix@HeaderDefaultTwo} +O{\skillMatrix@HeaderDefaultThree} +O{\skillMatrix@HeaderDefaultFour}}{%
    \begingroup
        \renewcommand{\arraystretch}{1.25}
        \arrayrulecolor{color1}
        \begin{tabular}{@{}p{\skillmatrix@hintscolumnwidth}@{\hspace{\separatorcolumnwidth}}%
                        p{\cvskill@width}@{\hspace{\skillmatrix@padding}}%
                        p{\cvskill@descriptorwidth}@{\hspace{\skillmatrix@padding}}%
                        p{\cvskill@experiencewidth}@{\hspace{\skillmatrix@padding}}
                        p{\skillmatrix@commentwidth}@{}}%
                & \centering{\cvSkillMatrix@HeadFont #2} & \centering{\cvSkillMatrix@HeadFont #3} & \centering{\cvSkillMatrix@HeadFont #4} & {\cvSkillMatrix@HeadFont #5}  
%                 \\\cdashline{2-5}[.6pt/1pt]
        \end{tabular}%
    \endgroup
    \par\addvspace{#1}
}
% in case moderncvbodyv is used we need a redefinition of \cvskillhead
\if@moderncvbodyv%
    \RenewDocumentCommand\cvskillhead{O{.25em} +O{\skillMatrix@HeaderDefaultOne} +O{\skillMatrix@HeaderDefaultTwo} +O{\skillMatrix@HeaderDefaultThree} +O{\skillMatrix@HeaderDefaultFour}}{%
        \arrayrulecolor{color1}%
        \setlength\arrayrulewidth{\separatorrulewidth}%
        \vspace*{-\separatorrulewidth}% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
        \begingroup%
    %     \renewcommand{\arraystretch}{1.25}%
        \begin{tabular}[t]{@{}p{\hintscolumnwidth}%
                        @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}%
                        p{\skillmatrix@hintscolumnwidth}%
                        @{\hspace{\skillmatrix@padding}}%
                        p{\cvskill@width}@{\hspace{\skillmatrix@padding}}%
                        p{\cvskill@descriptorwidth}@{\hspace{\skillmatrix@padding}}%
                        p{\cvskill@experiencewidth}@{\hspace{\skillmatrix@padding}}%
                        p{\skillmatrix@commentwidth}@{}}%
            & & \centering{\cvSkillMatrix@HeadFont#2} & \centering{\cvSkillMatrix@HeadFont#3} & \centering{\cvSkillMatrix@HeadFont#4} & {\cvSkillMatrix@HeadFont#5} \\[#1]% the spacing needs to be inside the cell for the vertical rule to extend correctly
        \end{tabular}%
        \endgroup%
        \par\@aftersectionfalse\ignorespaces%    
    }
\fi

% %-------------------------------------------------------------------------------
% %                \cvskillentry 
% %-------------------------------------------------------------------------------
\DeclareDocumentCommand\cvskillentry{s +O{} +m +m +m +m +m}{}%
% The following definition is valid for moderncvbodyi, moderncvbodyii, moderncvbodyiii, moderncvbodyiv
\RenewDocumentCommand\cvskillentry{s +O{.25em} +m +m +m +m +m}{%
    %test for the star * in the command
    \IfBooleanTF{#1}{% If a star is seen a dotted line is drawn above the entry
        \begingroup
        \renewcommand{\arraystretch}{1.25}
        \arrayrulecolor{color1}
        \begin{tabular}{@{}p{\skillmatrix@hintscolumnwidth}@{\hspace{\separatorcolumnwidth}}%
        p{\cvskill@width}@{\hspace{\skillmatrix@padding}}%
        p{\cvskill@descriptorwidth}@{\hspace{\skillmatrix@padding}}%
        p{\cvskill@experiencewidth} @{\hspace{\skillmatrix@padding}}%
        p{\skillmatrix@commentwidth}@{}}%
            \cdashline{2-5}[.6pt/1pt]
            \raggedleft\hintstyle{#3} &\centering \cvskill{#4} &\centering {#5} & \centering {#6} &{\itshape#7}%
        \end{tabular}%
        \endgroup
        \par\addvspace{#2}
    }{% If no star is seen no line is drawn
        \begin{tabular}{@{}p{\skillmatrix@hintscolumnwidth}@{\hspace{\separatorcolumnwidth}}%
        p{\cvskill@width}@{\hspace{\skillmatrix@padding}}%
        p{\cvskill@descriptorwidth}@{\hspace{\skillmatrix@padding}}%
        p{\cvskill@experiencewidth}%
        @{\hspace{\skillmatrix@padding}}%
        p{\skillmatrix@commentwidth}@{}}%
            \raggedleft\hintstyle{#3} &\centering \cvskill{#4} &\centering {#5} & \centering {#6} &{\itshape#7}%
        \end{tabular}%
        \par\addvspace{#2}
    }
}
% in case moderncvbodyv is used we need a redefinition of \cvskillentry
\if@moderncvbodyv%
    \DeclareDocumentCommand\@starIndependentMatrixEntry{}{}%
    \RenewDocumentCommand\cvskillentry{s O{.25em} +m +m +m +m +m}{%
        \arrayrulecolor{color1}%
        \setlength\arrayrulewidth{\separatorrulewidth}%
        \vspace*{-\separatorrulewidth}% HACK; I don't understand where the space is coming from, nor what it's exact value is :(
        %test for the star * in the command
        \RenewDocumentCommand{\@starIndependentMatrixEntry}{}{
            \begingroup%
                \renewcommand{\arraystretch}{1.25}%
                \begin{tabular}[t]{@{}p{\hintscolumnwidth}%
                                @{\hspace{\separatorcolumnwidth}}|@{\hspace{\separatorcolumnwidth}}p{\skillmatrix@hintscolumnwidth}
                                @{\hspace{\skillmatrix@padding}}
                                p{\cvskill@width}@{\hspace{\skillmatrix@padding}}%
                                p{\cvskill@descriptorwidth}@{\hspace{\skillmatrix@padding}}%
                                p{\cvskill@experiencewidth} @{\hspace{\skillmatrix@padding}}p{\skillmatrix@commentwidth}@{}}%
    %                 \cline{3-6}%
                    & \raggedleft\hintstyle{#3} &\centering \cvskill{#4} &\centering {#5} & \centering {#6} &{\itshape#7}\\[#2]%
                \end{tabular}%
            \endgroup%
        }
        \IfBooleanTF{#1}{% the star does not do anything here
            \@starIndependentMatrixEntry
        }{% 
            \@starIndependentMatrixEntry
        }%
        \par\@aftersectionfalse\ignorespaces%
    }
\fi
  
\endinput


%% end of file `moderncvskillmatrix.sty'.
